0

我在 index.html 的script标签中有一个函数,并且在index.html hideAppLoader()中有一个角度应用程序组件。我想在应用程序启动时调用该函数。我试过了,@Output但这不起作用。这是代码

索引.html

<head>
  <script>
     function hideAppLoader() { debugger;}
  </script>
</head>
<body>
  <my-app (onAppLoad)="hideAppLoader($event)"></my-app>
</body>

并在ts文件中

@Output() onAppLoad = new EventEmitter();

ngOnInit() {
  this.onAppLoad.emit();
}

但即使控制台上没有错误,该函数也不会被调用。我什至尝试(onAppLoad)="this.hideAppLoader($event)"(onAppLoad)="window.hideAppLoader($event)"

加载时,解决方案是什么,甚至可以从 Angular 组件调用纯 js 函数?

4

1 回答 1

2

由于脚本函数是全局的,因此您不需要使用 EventEmitter。只需将您更改ngOnInit为:

ngOnInit() {
  window.hideAppLoader();
}

从你的名字来看,hideAppLoader你可以在标签内定义加载器my-app,一旦 AppComponent 被加载,Angular 就会删除它。

<my-app>
  <img src="assets/loader" />
</my-app>
于 2018-05-03T12:24:55.087 回答