简体   繁体   English

Angular 2 无加载服务提供者

[英]Angular 2 No Provider for LoadingService

I've already read similar questions regarding this on StackOverflow, but they don't seem to be working for my situation.我已经在 StackOverflow 上阅读过类似的问题,但它们似乎不适用于我的情况。 I'm on the most recent version of Angular.我使用的是最新版本的 Angular。

Loader Component加载器组件

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 Service装载机服务

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;
  }
}

My error is: Error: Uncaught (in promise): No provider for LoadingService我的错误是:错误:未捕获(承诺):没有 LoadingService 提供者

Any help would be greatly appreciated.任何帮助将不胜感激。 Thank you谢谢

There is nothing wrong with the code you posted here. 您在此处发布的代码没有错。 I put it in my project and it loads without any issues. 我把它放在我的项目中,它加载没有任何问题。 Your issue is most likely your SystemJS configuration or some other issue that prevents the app from loading correctly, and therefore can't resolve how to inject your LoadingService. 您的问题很可能是您的SystemJS配置或其他导致应用程序无法正确加载的问题,因此无法解决如何注入LoadingService的问题。 Use Chrome's dev tools to look at the console when the app loads and see if it shows you any other issues. 在应用加载时,使用Chrome的开发工具查看控制台,查看是否显示其他问题。 Alternatively, post your full non-working sample somewhere we can actually see all the code (like Plunkr, or JSFiddle). 或者,将您的全部无效示例发布到我们可以实际看到所有代码的位置(例如Plunkr或JSFiddle)。

I faced a similar issue and solved it by importing the service in the providers specified in app.module.ts我遇到了类似的问题,并通过在 app.module.ts 中指定的提供程序中导入服务来解决它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM