uniapp小程序使用webview 嵌套 vue 项目

uniapp小程序使用webview 嵌套 vue 项目

小程序中发送


	<web-view :src="urlSrc" @message="handleMessage"></web-view>
	
	export default {
		data() {
			return {
				urlSrc: "",
			};
		},
		onLoad(options) {
		// 我需要的参数比较多 所以比较臃肿
		// 获取用户信息 根据自己需要
		let userInfor = uni.getStorageSync("userInfor") || ''
		// web-view url
		this.urlSrc = "https://xxxxxxxx.com/#/viewsEdit?key=" + options.id + "&srcurl=viewsEdit" +
				"&token=" + uni.getStorageSync('token') + "&wxopenid=" + uni.getStorageSync('wxopenid') + '&phone=' + userInfor
				.mobilePhone + "&name=" + userInfor.nickName + "&surveyId=" + options.ids
		}
	}

vue中接收参数

// index.html 中引入
    <script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>

 // App.vue中

<template>
  <div id="app">
    <RouterView v-if="isRouterAlive" />
  </div>
</template>

<script>
export default {
  mounted() {
  // 主要代码 开始
    let that = this
    console.log(window.location, 'this.$router.query')
     // 获取url 中的参数
        let datas = that.getUrlParams(window.location.href)
      if (datas.token) {
      // 保存token
        that.$store
          .dispatch('user/login', {
            token: 'bearer' + datas.token,
            ...datas
          })
          .then(() => {
            // 登录成功后路由跳回
            that.$router.replace({
              path: '/viewsEdit',
              query: {
                key: datas.key,
                wxopenid:datas.wxopenid,
                phone:datas.phone,
                name:datas.name,
                surveyId:datas.surveyId,
              }
            })
          })
  },
  methods: {
    getUrlParams(url) {
      const params = {}
      const reg = /([^?&=]+)=([^&]*)/g
      url.replace(reg, (match, key, value) => {
        params[decodeURIComponent(key)] = decodeURIComponent(value)
      })
      return params
    },
  }
}
</script>
// 使用uni提供的webview.js插件跳转小程序的页面
npm i uni-webview-lib 

vue发送消息给uniapp

//   viewsEdit.vue
<template>
  <div @click="submitForm">去小程序</div>
</template>

<script>
import {
  createFormResultRequest,
} from '@/api/project/data'
import uni from 'uni-webview-lib'
export default {
  methods: {
    submitForm() {
      createFormResultRequest().then((res) => {
        const message = '参数'
        uni.reLaunch({
          // 带上需要传递的参数
          url: `/subFishingBay/pages/palaceDraw/luckdraw?message=${message}&id=${res.data.id}`
        })
        this.msgSuccess('添加成功')
      })
    }
  }
}
</script>

小程序中接收数据

	 // 在上面跳转的页面  /subFishingBay/pages/palaceDraw/luckdraw
	 // luckdraw.vue
	onLoad(options) {
		 console.log(options,'这里是传过来的参数')
	},

搞定!

相关推荐

  1. uniapp程序使用webview 嵌套 vue 项目

    2024-07-11 04:52:05       24 阅读
  2. uniapp实现程序和内嵌webView的互通

    2024-07-11 04:52:05       52 阅读
  3. 前端UNIAPPwebview嵌入H5使用说明文档

    2024-07-11 04:52:05       33 阅读
  4. uniapp嵌套webview,无法返回上一级?

    2024-07-11 04:52:05       52 阅读
  5. uniapp程序项目解决键盘问题

    2024-07-11 04:52:05       21 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 04:52:05       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 04:52:05       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 04:52:05       57 阅读
  4. Python语言-面向对象

    2024-07-11 04:52:05       68 阅读

热门阅读

  1. iOS 开发中,异步渲染和异步绘制

    2024-07-11 04:52:05       18 阅读
  2. 请求被中止: 未能创建 SSL/TLS 安全通道

    2024-07-11 04:52:05       22 阅读
  3. 【LeetCode】字母异位词分组

    2024-07-11 04:52:05       20 阅读
  4. mybatis-plus树递归结构

    2024-07-11 04:52:05       22 阅读
  5. 一次业务的批量数据任务的处理优化

    2024-07-11 04:52:05       17 阅读
  6. 力扣之有序链表去重

    2024-07-11 04:52:05       24 阅读
  7. PyTorch DataLoader 学习

    2024-07-11 04:52:05       17 阅读
  8. 微生活服务平台与元宇宙的融合

    2024-07-11 04:52:05       19 阅读
  9. C++ 入门05:类和对象

    2024-07-11 04:52:05       26 阅读
  10. mysqli 与mysql 区别和联系, 举例说明

    2024-07-11 04:52:05       24 阅读
  11. SQL Server镜像与日志:数据保护的双重保障

    2024-07-11 04:52:05       19 阅读
  12. 系统设计题-路由表最长匹配

    2024-07-11 04:52:05       22 阅读
  13. springboot+vue项目实战2024第三集修改用户信息

    2024-07-11 04:52:05       26 阅读