內嵌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