内嵌H5与原生app的交互,函数的相互调用


判断是否是原生
isNative() {
      let equipmentType = "";
      let agent = navigator.userAgent.toLowerCase();
      let android = agent.indexOf("android");
      let iphone = agent.indexOf("iphone");
      let ipad = agent.indexOf("ipad");
      if (android != -1) {
        equipmentType = "android";
      }
      if (iphone != -1 || ipad != -1) {
        equipmentType = "ios";
      }
      return equipmentType;
    },

    //跳转原生 其中跳转方法需与原生沟通

    goNative(val) {
      let params = {
        pageType: val,
        startTime: this.startTime+' 00:00:00',
        endTime: this.endTime+' 23:59:59',
      };
      if (this.isNative() === "android") {
        console.log("android", JSON.stringify(params));
        window.android.jumpPage(JSON.stringify(params));
      } else if (this.isNative() === "ios") {
        console.log("ios111", JSON.stringify(params));
        window.webkit.messageHandlers.jumpPage.postMessage(JSON.stringify(params));
      }
    },

当app调用vue中的方法不生效时:在app端内嵌页面中调用vue中的函数时,拿不到methods中定义的函数,需要将Vue项目methods中的方法在mounted中暴露在window上

APP端调用咱们的方法都是绑定在window上面的方法,故咱们应该讲咱们的方法暴露在window上面,因为Vue项目中的this指向vue,而不是window

  mounted() {
    window["appPushToken"] = (userInfo) => {
      this.AppInfo = userInfo;
      //判断是不是APP端
      if (this.AppInfo) {
        this.AppInfoPlatform = true;
      } else {
        this.AppInfoPlatform = false;
      }
      this.appShare('1')
    };
    window['shareInfo'] = () => {
        this.shareInfo()
     }
  },
 

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM