js调用ts,亲测。

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)

©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页