ndfweb.cn

小程序与H5如何互相跳转


2021-07-31 23:31:42 (6282)


转自链接:https://www.jianshu.com/p/22a5dab598ce

小程序跳转H5

需要用到小程序的web-view,官方文档链接

web-view是承载网页的容器。会自动铺满整个小程序页面,个人类型的小程序暂不支持使用。写法如下:

<view class="page-body">
    <web-view src="https://xxx.com/test.html"></web-view></view>

注:当在微信开发中工具里返回“{"base_resp":{"ret":-1}}”时,需要点左上角“设置”--“项目设置”--勾选“不校验合法域名、web-view(业务域名)、TLS 版本以及 HTTPS 证书”

无标题.png

H5跳转小程序

因为外部h5无法跳转到小程序,因此需要把h5内嵌到小程序的web-view中。
一:首页小程序内嵌h5网页,内嵌这一步就相当于上面的小程序跳转h5:

<view class="page-body">
    <web-view src="https://xxx.com/test.html"></web-view></view>

二:然后在内嵌的网页里引入js,调用wx.miniProgram.navigateTo跳转小程序方法,可在url后拼接要传的参数:

<!DOCTYPE html><html>
    <head>
        <meta charset="UTF-8">
        <title>h5跳转小程序</title>
    </head>
    <body>
        <h3 align="center">正在跳转到小程序...</h3>
        <script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
        <script>
                wx.miniProgram.navigateTo({url: '/index/index?phone=18012121212'})        </script>
    </body></html>

三:小程序接受参数的页面:
index.wxml:

<view class="page-body">{{phone}}</view>

index.js

Page({
  data: {
    phone:''
  },

  onLoad: function (options) {
    var that = this;
    /*获取参数*/
    var phone = options.phone
    that.setData({
      phone: phone,
    })
  }})

这样就从h5跳到小程序指定的页面并且可以拿到我们想要传的参数

关于web-view相关的接口:

官方js调用方法示例:

<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>// javascriptwx.miniProgram.navigateTo({url: '/path/to/page'})wx.miniProgram.postMessage({data: 'foo'})wx.miniProgram.postMessage({data: {foo: 'bar'}})wx.miniProgram.getEnv(function (res) { console.log(res.miniprogram) })





了解更多请访问:http://www.ndfweb.cn/news-883.html
  NDF俱乐部
  国际域名注册
  建站咨询
合作伙伴:万网 | 新网 | 新网互联 NDF网站建设淘宝店 | 实用工具 | 外貿網站建設 | 联系我们
鲁公网安备 37110202000336号 鲁ICP备2021027697号-1 Sitemap - RSSRSS订阅