1.先在项目src目录下定义全局方法 clobal.d.ts
declare var playOver: any; declare var getAudioUrl: any; declare var authType: any; declare var isDeleted: any; interface Window { playOver: any; } interface Window { getAudioUrl: any; } interface Window { authType: any; } interface Window { isDeleted: any; }2.在ts里写全局方法
ngOnInit() { this.planId = this.activatedRoute.snapshot.queryParamMap.get("belongId")snapshot.queryParamMap.get("pattern"); this.rebuild = this.activatedRoute.snapshot.queryParamMap.get("rebuild"); this.openId = this.cookiesUtilService.getOpenId(); this.audio = document.querySelector('audio'); this.stop = document.getElementById('stop'); this.htmlDetail(); let that = this; //全局方法palyOver 安卓和iOS都调用这个方法 全局方法里我在调用了移动端方法,这样就实现互调 window['playOver'] = function () { alert(that.recordTime); that.playing = false; if (window["plan"]) { window["plan"].playIsOver(); } if (window["webkit"]) { window["webkit"].messageHandlers.playIsOver.postMessage({ item: '' }); } }; //全局方法getAudioItems window['getAudioItems'] = function (item: any) { alert("获取app传来的语音信息"); console.log(item); that.audioItems = item; }; //全局方法authType window['authType'] = function (item: any) { alert("获取权限状态,1授权,0没授权"); that.typeOfAuth = item; console.log(item); if (that.typeOfAuth == 1) { that.setRecordingTime(that.activeId); } }; }