[英]When injecting(importing) a service into my constructor I get a "No provider" error
Angular 2 - When injecting(importing) a service into my constructor I get a "No provider" error. Angular 2 - 将服务注入(导入)到我的构造函数中时,出现“无提供者”错误。
Here's my service I'm importing:这是我要导入的服务:
export class EnvironmentService {
public baseurl: string = "/baseurl/example/";
}
Here's my class with the error:这是我的班级有错误:
import {Http, Headers} from "angular2/http";
import {Injectable} from 'angular2/angular2';
import {EnvironmentService} from './environmentService';
@Injectable()
export class AuthService {
constructor(private _environmentService: EnvironmentService) {
//This is where the error is.
console.log(this._environmentService);
}
isUserLoggedIn = (): Promise<boolean> => {
return new Promise((resolve, reject) => {
resolve(true);
})
}
}
在app.module.ts
Providers 中添加AuthService
Most likely you didn't specify EnvironmentService
as a provider for your application.很可能您没有将
EnvironmentService
指定为应用程序的提供者。 You can do it by passing all providers in a second argument of the bootstrap
function, or using providers
and viewProviders
properties of the Component
decorator:您可以通过在
bootstrap
函数的第二个参数中传递所有提供者来实现,或者使用Component
装饰器的providers
和viewProviders
属性:
bootstrap(AppComponent, [EnvironmentService /*, other providers */]);
// or
@Component({
// ... component config
providers: [EnvironmentService],
// or
viewProviders: [EnvironmentService]
})
class AppComponent { /* ... */ }
GO to the module that you want to use the EnvironmentService
ie app.module.ts
or other modules.转到您要使用
EnvironmentService
的模块,即app.module.ts
或其他模块。
after imports you will see provider as an array add the EnvironmentService导入后,您将看到 provider 作为数组添加 EnvironmentService
providers: [AuthService]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.