我就是这样做的。我的组件如下。不要忘记导入 NgZone。这是这里最重要的部分。NgZone 让 angular 理解外部环境。通过 zone 运行函数允许您从在 Angular zone 之外执行的任务重新进入 Angular zone。我们在这里需要它,因为我们正在处理不在角度区域内的外部呼叫。
 import { Component, Input , NgZone } from '@angular/core';
 import { Router } from '@angular/router';
    @Component({
        selector: 'example',
        templateUrl: './example.html',
    })
    export class ExampleComponent {
            public constructor(private zone: NgZone, private router: Router) {
//exposing component to the outside here
//componentFn called from outside and it in return calls callExampleFunction()
        window['angularComponentReference'] = {
            zone: this.zone,
            componentFn: (value) => this.callExampleFunction(value),
            component: this,
        };
    }
    public callExampleFunction(value: any): any {
        console.log('this works perfect');
        }
    }
现在让我们从外部调用它。在我的情况下,我想通过我的 index.html.my index.html 的脚本标签到达这里,如下所示。
<script>
//my listener to outside clicks
ipc.on('send-click-to-AT', (evt, entitlement) => 
electronClick(entitlement));;
//function invoked upon the outside click event
 function electronClick(entitlement){
//this is the important part.call the exposed function inside angular 
//component
    window.angularComponentReference.zone.run(() =
    {window.angularComponentReference.componentFn(entitlement);});
 }
</script>
如果您只是在开发人员控制台中键入以下内容并按回车键,它将调用公开的方法,并且“这很完美”将打印在控制台上。
 window.angularComponentReference.zone.run(() =>
{window.angularComponentReference.componentFn(1);});
权利只是在此处作为参数传递的一些值。