移动端h5: ----h5页面检测小程序环境

时间:2022-10-07 00:08:26

1. 照例先引入微信桥接代码:

 <script src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>

2. 自定义js方法进行判断:

const ua = window.navigator.userAgent.toLowerCase();
      if (ua.indexOf("micromessenger") == -1) {
        //非微信环境
        this.isInWX = false;
      } else {
        this.isInWX = true;
        wx.miniProgram.getEnv(res => {
          if (res.miniProgram) {
            //小程序环境
            this.isInWXAPP = true;
          } else {
            //非小程序环境
            this.isInWXAPP = false;
          }
        });
      }

 原文链接: https://blog.csdn.net/sxtysjzx0606/article/details/82427346