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,'这里是传过来的参数')
	},

搞定!

相关推荐
汝生淮南吾在北17 小时前
SpringBoot+Vue超市收银管理系统
vue.js·spring boot·后端
p***930317 小时前
SpringBoot + vue 管理系统
vue.js·spring boot·后端
8***v25718 小时前
SpringBoot + vue 管理系统
vue.js·spring boot·后端
tsumikistep19 小时前
【前后端】Vue 脚手架与前端工程结构入门指南
前端·javascript·vue.js
华仔啊19 小时前
Vue3 + Element Plus 动态菜单实现:一套代码完美适配多角色权限系统
前端·vue.js
多看书少吃饭19 小时前
小程序支持HTTP POST 流式接口吗?
网络协议·http·小程序
慧慧吖@20 小时前
关于在本地去模拟生产环境检测页面内容注意事项
前端·javascript·vue.js
黄团团20 小时前
Vue2整合Electron开发桌面级应用以及打包发布(提供Gitee源码)
前端·javascript·vue.js·elementui·electron
独角仙梦境21 小时前
同事:架构太复杂了,源码文件找半天。 我:源码溯源了解一下?
前端·vue.js
我叫张小白。1 天前
Vue3 路由:单页面应用的核心引擎
前端·javascript·vue.js·前端框架·vue3