简体   繁体   English

将服务注入(导入)到我的构造函数中时,出现“无提供者”错误

[英]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装饰器的providersviewProviders属性:

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.

相关问题 Angular2-将服务注入服务时出现无效的提供程序错误 - Angular2 - Invalid provider error when injecting service into service 将自定义过滤器注入服务时的“未知提供者” - “Unknown Provider” when injecting custom filter into service 离子未捕获错误:导入服务提供商时找不到模块“。” - Ionic Uncaught Error: Cannot find module “.” when importing a service provider 在控制器中注入服务时出错 - error when injecting service in controller angularjs 1.5在组件中注入服务 - 未知的提供程序错误 - angularjs 1.5 injecting service in a component - uknown provider error AngularJS:未知提供者:$ scopeProvider &lt; - $ scope error(不在服务中注入$ scope) - AngularJS: Unknown provider: $scopeProvider <- $scope error (not injecting $scope in the service) 将自定义服务注入其他模块时,AngularJS中的未知提供程序 - Unknown Provider in AngularJS when Injecting custom service into different module Angular,将自己的服务注入提供者 - Angular, injecting own service into provider 在配置块中注入服务或工厂时出错 - Error when injecting a service or factory in config block 为 React Typescript 导入包时不是构造函数错误 - Not a constructor error when importing packages for React Typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM