为什么找不到我的提供商/服务?

时间:2016-11-11 07:12:03

标签: angular systemjs

据我所知,我已跟随docs创建CoreModule以在Angular 2.0.0中提供我的服务。我也尝试过以下建议:

Provide core singleton services module in Angular 2

Angular2 RC5 ngModule: No Provider For NameService

无济于事。什么是获得服务"有线连接"?我通过以下代码得到的错误是:

  

没有AuthService的提供者!

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppRoutingModule } from './app-routing.module';

//Modules/Components
import { AppComponent } from './app.component';
import { SignInModule } from './sign-in/signin.module';
import { CoreModule } from './services/core.module';

@NgModule({
    imports: [BrowserModule, AppRoutingModule, FormsModule,
        CoreModule, SignInModule],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
})
export class AppModule { }

服务/ core.module.ts

import { NgModule } from '@angular/core';

// Services
import { SignalRService } from './signalr.service';
import { AuthService } from './auth.service';
import { UserSettingsService } from './user-settings.service';

@NgModule({
    providers: [SignalRService, AuthService, UserSettingsService]
})
export class CoreModule {
}

服务/ auth.service.ts

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { SignalRService } from './signalr.service';
import { ISignInProvider } from '../sign-in/signin.interface';

@Injectable()
export class AuthService {
    constructor(private signalR: SignalRService, private router: Router) {
    }
..
}

登录入/ signin.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { GoogleSignInComponent } from './google-signin.component';

@NgModule({
    imports: [CommonModule, FormsModule],
    declarations: [GoogleSignInComponent],
    exports: [GoogleSignInComponent],
})
export class SignInModule { }

登录/ google-signin.component.ts (我认为这是错误指向的地方)

import { Component, AfterViewInit } from '@angular/core';
import { AuthService } from '../services/auth.service';
import { ISignInProvider } from './signin.interface';

@Component({
    moduleId: module.id,
    selector: 'google-signin',
    templateUrl: 'google-signin.component.html',
})
export class GoogleSignInComponent implements AfterViewInit, ISignInProvider     {
    constructor(private authService: AuthService) {
    }
}

组件中的登录名位于功能模块(SignInModule)中,但是已在应用模块中导入。我尝试在forRoot中使用CoreModule,但似乎没有效果。我不能按照另一个问题的建议让SystemJS别名工作,但我不确定为什么这会解决任何问题。

不使用CoreModule并将提供商直接放在AppModule上也不起作用。我没有把它们直接放在组件上,因为我的理解是这是不好的做法。就我所知,登录组件/模块并不是延迟加载的,并且它不是路由器的一部分(它位于应用程序组件模板中)。

我的错误在哪里,或者我应该以不同的方式做什么?

1 个答案:

答案 0 :(得分:2)

您必须在SignalRService AuthServiceUserSettingsServiceimport { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; import { GoogleSignInComponent } from './google-signin.component'; @NgModule({ imports: [CommonModule, FormsModule], declarations: [GoogleSignInComponent], providers: [SignalRService, AuthService, UserSettingsService], exports: [GoogleSignInComponent], }) export class SignInModule { } 内添加提供商。

SignalRService

或者您必须创建一个共享模块,该模块具有AuthServiceUserSettingsService//SharedModule import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; import { GoogleSignInComponent } from './google-signin.component'; @NgModule({ imports: [CommonModule, FormsModule], providers: [SignalRService, AuthService, UserSettingsService] }) export class SharedModule { } //SignInModule import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; import { SharedModule } from 'path to SharedModule'; import { GoogleSignInComponent } from './google-signin.component'; @NgModule({ imports: [CommonModule, FormsModule, SharedModule], declarations: [GoogleSignInComponent], exports: [GoogleSignInComponent], }) export class SignInModule { } 的提供者声明,您必须在所有其他模块中导入此共享模块。

{{1}}
相关问题