我正在尝试实现一个全局加载指示器,可以在整个应用程序中重复使用。我有一个可注入的服务,其中包含show和hide函数:
这是spinner遮罩组件的代码。我将省略有关HTML和CSS实现的细节,因为它们在这里并不重要。
问题:在叠加组件的代码中,我正在订阅服务的可观察loaderState。然而,当我调用show()函数触发可观察对象的next()时,订阅回调没有被触发。
这是我在app.component.ts中调用show()函数的方式:
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable()
export class SpinnerOverlayService {
private loaderSubject = new Subject<any>();
public loaderState = this.loaderSubject.asObservable();
constructor() { }
/**
* Show the spinner
*/
show(): void {
this.loaderSubject.next(<any>{ show: true });
}
/**
* Hide the spinner
*/
hide(): void {
this.loaderSubject.next(<any>{ show: false });
}
}
这是spinner遮罩组件的代码。我将省略有关HTML和CSS实现的细节,因为它们在这里并不重要。
import { Component, OnInit } from '@angular/core';
import { Subscription } from 'rxjs';
import { SpinnerOverlayService } from '../spinner-overlay.service';
@Component({
selector: 'spinner-overlay',
templateUrl: './spinner-overlay.component.html',
styleUrls: ['./spinner-overlay.component.scss']
})
export class SpinnerOverlayComponent implements OnInit {
show = false;
private _subscription: Subscription;
constructor(private spinnerOverlayService: SpinnerOverlayService) { }
ngOnInit(): void {
this._subscription = this.spinnerOverlayService.loaderState.subscribe((state) => {
console.log("Subscription triggered.");
this.show = state.show;
});
}
ngOnDestroy(): void {
this._subscription.unsubscribe();
}
}
问题:在叠加组件的代码中,我正在订阅服务的可观察loaderState。然而,当我调用show()函数触发可观察对象的next()时,订阅回调没有被触发。
这是我在app.component.ts中调用show()函数的方式:
ngOnInit() {
this.spinnerOverlayService.show();
}
我可能错过了什么?回调没有触发似乎很奇怪。
以下是在 Stackblitz 中的示例:https://stackblitz.com/edit/angular-7-registration-login-example-2qus3f?file=app%2Fspinner-overlay%2Fspinner-overlay.component.ts