0
我已經閱讀過關於StackOverflow的類似問題,但他們似乎沒有爲我的情況工作。我正在使用Angular的最新版本。Angular 2沒有提供加載服務
Loader組件
import {Component, ElementRef, OnInit, OnDestroy} from '@angular/core';
import {CORE_DIRECTIVES} from "@angular/common";
import {LoadingService} from "../../services/global/loadingService";
@Component({
selector: 'loader',
directives: [CORE_DIRECTIVES],
providers: [LoadingService],
template: `
<div class ="blue"></div>
`,
})
export class LoadingComponent {
public active: boolean;
public constructor(spinner: LoadingService) {
spinner.status.subscribe((status: boolean) => {
this.active = status;
});
}
}
Loader服務
import {Injectable} from '@angular/core';
import {Subject} from 'rxjs/Subject';
import 'rxjs/add/operator/share';
@Injectable()
export class LoadingService {
public status: Subject<any> = new Subject();
private _active: boolean = false;
public get active(): boolean {
return this._active;
}
public set active(v: boolean) {
this._active = v;
this.status.next(v);
}
public start(): void {
this.active = true;
}
public stop(): void {
this.active = false;
}
}
我的錯誤是:錯誤:未捕獲的(在承諾):無提供LoadingService!
任何幫助將不勝感激。謝謝!
您不需要列出'CORE_DIRECTIVES'。它們默認全球可用。不知道你的問題。代碼看起來很好。 –