【问题标题】:Angular 2 No Provider for LoadingServiceAngular 2 没有 LoadingService 的提供者
【发布时间】:2016-08-12 09:55:35
【问题描述】:

我已经在 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 的提供者!

任何帮助将不胜感激。谢谢!

【问题讨论】:

  • 您不需要列出CORE_DIRECTIVES。默认情况下,它们在全球范围内可用。不知道你的实际问题。代码看起来不错。

标签: javascript html service angular typescript


【解决方案1】:

您在此处发布的代码没有任何问题。我把它放在我的项目中,它加载没有任何问题。您的问题很可能是您的 SystemJS 配置或其他阻止应用程序正确加载的问题,因此无法解决如何注入您的 LoadingService。使用 Chrome 的开发工具在应用加载时查看控制台,看看它是否显示任何其他问题。或者,将完整的非工作示例发布到我们实际可以看到所有代码的地方(如 Plunkr 或 JSFiddle)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    • 2015-07-26
    • 2018-01-09
    • 1970-01-01
    • 2017-02-14
    • 2018-08-10
    • 1970-01-01
    相关资源
    最近更新 更多