ionic之index.html,及使用js调用ts的class

  因为项目上的原因,需要在index.html中加入许多东西,但是那些请求关系,数据关系错综复杂,难以在index.html中进行使用。这就需要将ts引入到js中。网上查询了一下,主要方法就是将ts的class做一个全局window下的静态对象。

    首先,index.html中的标签id是可以传给pages中的ts的class中的,因为在index.html中其实是包含了<ion-app></ion-app>与其他一些标签,只不过把pages放入了ion-app,范围是一致的。

    最重要的是如何使用index.html使用ts的class,其实class new后为一个实例,只要将该实例变为单静态实例传给全局window即可以js来使用,这样也不用怕该实例是否改变了一些什么东西。具体实现其实非常简单,与java想法类似。

    export class LiveRadioPage  {
      private static s_instance: LiveRadioPage = null;

      private platformInstance_LiveRadioPage: LiveRadioPage;

      constructor(
        public toastCtrl: ToastController
        ) {

          this.init();

        }

      public init(){
        LiveRadioPage.s_instance = this;
        window['platformInstance_LiveRadioPage'] = LiveRadioPage.s_instance;

      }

    }

在js中利用window.platformInstance_LiveRadioPage.函数名进行调用函数

ts中调用ts同理可用window['platformInstance_LiveRadioPage'] .函数名进行调用
————————————————
版权声明:本文为CSDN博主「qq_24802531」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_24802531/article/details/80603992

猜你喜欢

转载自www.cnblogs.com/telwanggs/p/12094753.html