js呼叫ts,親測。

住幼兒園的楊先生發表於2020-09-26

ts類:

export class OperateStepsPage implements OnInit {
private static jsUse: OperateStepsPage = null;
private operateStepsPage: OperateStepsPage;
constructor(
public alertController: AlertController,
public activatedRoute: ActivatedRoute,
public router: Router,
public eventService: EventService,
) {
this.init();
}
public init() { // 做一個全域性註冊
OperateStepsPage.jsUse = this;
// 若下面window[‘operateStepsPage’]提示:object access via string literals is disallowed(不允許通過字串文字訪問物件),可先定義再訪問
// 若不提示錯誤,可省略定義:const operateStepsPage= ‘operateStepsPage’;直接window[‘operateStepsPage’]
const operateStepsPage = ‘operateStepsPage’;
window[operateStepsPage] = OperateStepsPage.jsUse;
}
ccc() { // 將被js 呼叫的ts方法
console.log(‘我是ts’);
}
}
js:

function jstest() {
console.log(111);
window[‘operateStepsPage’].ccc()
}
結果;

111
我是ts
以上內容本人親測,程式碼均為原創,如有轉載請註明來源(洋蔥先生: http://www.dreamload.cn/blog/?p=945)

相關文章