如何在我的 ios 委托 class 中访问组件方法

How to access component method inside my ios delegate class

如何从应用程序组件访问方法到 main.ts 文件?

我正在使用 Nativescript angular 开发 android 和 ios 移动应用程序。我为应用程序 onResume 创建了一个 ios 委托 class。我想在我的 ios 委托 class 中调用本机脚本 class 组件方法。帮我实现功能

My ios delegate class:
import { android, ios, AndroidApplication, AndroidActivityBundleEventData } from "tns-core-modules/application";
import * as application from "tns-core-modules/application";



    class MyDelegate1 extends UIResponder implements UIApplicationDelegate{


        constructor(private sharedClass:sharedClass) {
            super();
        }

        public static ObjCProtocols =[UIApplicationDelegate];

        applicationWillEnterForeground(application:UIApplication):void{
                application.notify({
                    eventName:"SampleFunction",
                    object:application
                }) 


        }

        applicationDidEnterBackground(application:UIApplication):void{
            console.log("applicationDidEnterBackground"+application);
        }
    }



我的 nativescript 组件 class

    import { Component, OnInit } from "@angular/core";

    export class AppComponent implements OnInit {

      constructor(){}

      ngOnInit(){ }

       SampleFunction(){
         console.log("Will call from ios delegate class");
        }

}

帮我解决这个问题。

ViewChild 可能是您正在寻找的方式之一。

在你的app.component.ts,

import {ViewChild} from "@angular/core";

export class AppComponent{

   @ViewChild('templateReferenceVariableName') reference : YourComponent;

   reference.methodYouWantedToAccess();
}

在你的app.component.html,

<html>
    <your-component #templateReferenceVariableName > </your-component>
</html>

#templateReferenceVariableName 充当 .ts.html[=12 之间的 link =]

P.S: 名字可以换成你喜欢的名字

您不应该直接从您的委托调用 Component 方法。更好的做法是,在应用程序对象上触发一个事件并收听 Angular 组件的相同形式。

仅供参考,开箱即用地支持暂停/恢复等事件。

编辑:

要在应用程序模块上触发事件,

   import * as application from "tns-core-modules/application";

    applicationWillEnterForeground(application:UIApplication):void{
        application.notify({
            eventName: "YourEventName",
            object: application,
            // Add any additional params that can be accessed from the event object by listener callback
        });         
    }