3

我想在调用 Angular 2 中的引导方法以在页面加载时引导应用程序之前发出 AJAX 请求。

有没有我可以使用 Angular 来监听的钩子?我知道我可以将它包装在一个普通的 javascript ajax 请求中,但我认为可能有一种使用 Angular 2 执行此操作的标准方法。在启动 Angular 2 应用程序之前,我需要从服务器获取配置文件。

4

1 回答 1

2

据我所知,Angular 没有为这个用例提供一个特殊的钩子。
一个简单的“解决方法”是只用一个包装根组件模板的内容ngIf

@Component({
  selector: 'my-app',
  template: `
<div *ngIf="_config">
  ...
</div>`
...
)}
export class MyApp {
  private _config;
  constructor(configService:ConfigService) {
    configService.change.subscribe(value) => {
      this.config = value;
    });
  }
}
于 2016-02-10T07:06:18.207 回答