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 小时前
Element-Plus:popconfirm与tooltip一起使用不生效?
前端·vue.js·element
前端梭哈攻城狮1 小时前
uniapp图片上传添加水印/压缩/剪裁
前端·javascript·vue.js
踢足球的,程序猿3 小时前
从 Vue 2.0 进阶到 Vue 3.0 的核心技术解析指南
前端·javascript·vue.js·前端框架·html
掘金安东尼4 小时前
仅仅是发送一封邮件?暴露安全边界!
javascript·vue.js·面试
bo521004 小时前
突破性能瓶颈:基于虚拟滚动的大数据需求文档方案——告别卡顿与分页,实现Word级流畅体验
javascript·vue.js
小张快跑。4 小时前
【Vue3】(三)vue3中的pinia状态管理、组件通信
前端·javascript·vue.js
一颗奇趣蛋5 小时前
vue性能优化(响应数据&静态数据)
vue.js·性能优化
Hilaku5 小时前
用好了 defineProps 才叫会用 Vue3,90% 的写法都错了
前端·javascript·vue.js
Dignity_呱5 小时前
vue3对组件通信做了哪些升级?
前端·vue.js·面试
独立开阀者_FwtCoder6 小时前
最全301/302重定向指南:从SEO到实战,一篇就够了
前端·javascript·vue.js