小程序 web-view 如何给外链接的 h5 页面传递参数和值

小程序 web-view 如何给外链接的 h5 页面传递参数和值

最近做项目的时候发现这种方法还是比较方便的。类似小程序页面之间的传值形式,举个例子:

<web-view src="{{url}}?id={{id}}&token={{token}}&member={{member}}&paused={{paused}}" ></web-view>

然后在链接的 h5 页面使用正则解析出 src 中所带的一些参数和值,比如下面这段代码:

  created() {
    console.log(window.location.search.substr(1))
    this.id = this.getQueryString("id") || "0";
    this.token = this.getQueryString("token") || "";
    this.member = this.getQueryString("member") || "true";
    this.paused = this.getQueryString("paused") || "true"; //是否暂停
  }

  getQueryString: function(name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
      var r = window.location.search.substr(1).match(reg);
      if (r != null) {
        return decodeURIComponent(r[2]);
      }
      return null;
    }

猜你喜欢

转载自blog.csdn.net/Gochan_Tao/article/details/80342581