[英]Angular 2 No Provider for LoadingService
我已經在 StackOverflow 上閱讀過類似的問題,但它們似乎不適用於我的情況。 我使用的是最新版本的 Angular。
加載器組件
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;
});
}
}
裝載機服務
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 提供者
任何幫助將不勝感激。 謝謝
您在此處發布的代碼沒有錯。 我把它放在我的項目中,它加載沒有任何問題。 您的問題很可能是您的SystemJS配置或其他導致應用程序無法正確加載的問題,因此無法解決如何注入LoadingService的問題。 在應用加載時,使用Chrome的開發工具查看控制台,查看是否顯示其他問題。 或者,將您的全部無效示例發布到我們可以實際看到所有代碼的位置(例如Plunkr或JSFiddle)。
我遇到了類似的問題,並通過在 app.module.ts 中指定的提供程序中導入服務來解決它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.