实现这一点的最佳方法是创建一个可观察的来管理加载状态。我更喜欢用
BehaviorSubject
因为它记住它发出的最后一个值。您可以让多个组件订阅加载服务。(更多关于行为主题
here
)
这里有一个到StackBlitz的链接,我在这里实现了这个加载程序服务的一个简单版本:
https://stackblitz.com/edit/angular-loading-service-subscribe
编辑:我添加了
child.component
答案。我在Stackblitz上有,但忘了在这里加。
关键注意事项:
loading.service.ts
有一个
行为主体
它管理应用程序是否正在加载数据。它自己回来了
.asObservable()
所以没有子组件可以调用
.next()
关于它。
export class LoadingService {
private _isLoading$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(true);
get isLoading$(): Observable<boolean> {
return this._isLoading$.asObservable();
}
// see the rest of the code in the stackblitz
}
连接好后,将其注入组件。
app.component.ts
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
// by making it public, you can subscribe in the template
constructor(public loadingService: LoadingService) { }
}
app.component.html
<ng-template [ngIf]="loadingService.isLoading$ | async" [ngIfElse]="loaded">
Loading
</ng-template>
<ng-template #loaded>
Content has loaded
</ng-template>
<!-- see child.component -->
<child></child>
然后,child.component可以使用
loading.service
.
@Component({
selector: 'child',
template: `
<h1>Child Component</h1>
<button (click)="toggleLoading()">Change Loading</button>
`,
styles: [`h1 { font-family: Lato; }`]
})
export class ChildComponent {
constructor(public loadingService: LoadingService) { }
// simulates a 3 second load time for fetching data
ngOnInit() {
this.loadingService.startLoading();
setTimeout(() => {
this.loadingService.stopLoading();
}, 3000);
}
// simple method to simulate user action that loads some new data
toggleLoading(): void {
this.loadingService.toggleLoading();
}
}
查看此资源以获取有关
ng-template
:
https://toddmotto.com/angular-ngif-else-then
这个是为了
async
管道:
https://angular.io/api/common/AsyncPipe