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 小时前
JAVA代泊车接机送机服务代客泊车系统源码支持小程序+APP+H5
java·开发语言·微信小程序·小程序
哆啦A梦15887 小时前
搜索页面布局
前端·vue.js·node.js
_院长大人_8 小时前
el-table-column show-overflow-tooltip 只能显示纯文本,无法渲染 <p> 标签
前端·javascript·vue.js
SevgiliD8 小时前
el-table中控制单列内容多行超出省略及tooltip
javascript·vue.js·elementui
软件技术员8 小时前
微信小程序电子测宅堪墓风水罗盘
微信小程序·小程序
future_studio8 小时前
聊聊 Unity(小白专享、C# 小程序 之 播放器)
unity·小程序·c#
Q_Q5110082859 小时前
python+uniapp基于微信小程序的旅游信息系统
spring boot·python·微信小程序·django·flask·uni-app·node.js
哆啦A梦15889 小时前
axios 的二次封装
前端·vue.js·node.js
小Tomkk9 小时前
Rokid 开发空间小程序 实战
3d·小程序·rokid·jsar
阿珊和她的猫9 小时前
深入理解与手写发布订阅模式
开发语言·前端·javascript·vue.js·ecmascript·状态模式