内嵌H5与原生app的交互,函数的相互调用 navigator.userAgent浏览器检测
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