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

Angular 为什么找不到我的提供商/服务?,angular,systemjs,Angular,Systemjs,据我所知,我已经按照创建了一个CoreModule,以在Angular 2.0.0中提供服务。我还尝试了以下方面的建议: 无济于事。让服务“连接起来”需要什么?我在使用以下代码时遇到的错误是: 没有AuthService的提供程序 应用程序模块.ts import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms'; import { AppRouti

据我所知,我已经按照创建了一个
CoreModule
,以在Angular 2.0.0中提供服务。我还尝试了以下方面的建议:

无济于事。让服务“连接起来”需要什么?我在使用以下代码时遇到的错误是:

没有AuthService的提供程序

应用程序模块.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 { }
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 {
}
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) {
    }
..
}
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 { }
服务/core.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 { }
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 {
}
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) {
    }
..
}
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 { }
服务/auth.service.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 { }
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 {
}
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) {
    }
..
}
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 { }
登录/登录模块.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 { }
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 {
}
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) {
    }
..
}
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(我想这就是错误所在)

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

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


我的错误在哪里?或者,我应该采取什么不同的做法?

您必须在
signin.module.ts
中为
信号服务
AuthService
用户设置服务
添加提供者

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],
    providers: [SignalRService, AuthService, UserSettingsService],
    exports: [GoogleSignInComponent],
})
export class SignInModule { }
或者,您必须创建一个共享模块,其中包含
SignalRService
AuthService
usersetings服务的提供者声明,并且您必须在所有其他模块中导入此共享模块

//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 { }

向组件添加提供程序并不是一种坏做法。这取决于你真正想要完成什么。如果要限制服务的范围,必须使用延迟加载的模块或将其添加到组件中。我没有看到你的代码中有错误。我希望它能工作。@GünterZöchbauer我不想限制范围,这些服务在整个应用程序中都使用,所以我希望这个方法能工作。如果让它工作的唯一方法是走组件路线,我会的。这应该只对指令、组件和管道是必要的,而不是对服务(不过我自己在这方面还没有做太多调查)。我相信文档明确地说不要重新导入CoreModule。。。我是否遗漏了什么或者文档是否过时了?提供程序(延迟加载的模块除外)总是添加到根DI范围,因此在一个位置导入模块就足够了。在它所依赖的每个模块中导入一个模块可能是一个很好的做法,以使依赖性变得明显(使指令可用,如前所述),但我认为这不应该是必要的。AFAIR文档中提到,如果在多个地方导入这样一个模块,则提供程序将被删除,并且只有一个提供程序将在根作用域中注册。@GünterZöchbauer除了重新导入之外,我注意到他的
SharedModule
导出服务并将其放入提供程序阵列中,这是正确的,还是应该只在提供者中?另外,我要再次确认,我链接的文档中的这句话不再正确了?“只有根AppModule应该导入CoreModule。如果延迟加载的模块导入它,则会发生不好的事情。”或者我需要导入,因为我的其他模块不是延迟加载的?“不好的事情”这就是2个实例,因为延迟加载的模块有自己的根作用域,延迟加载的组件和服务不获取全局实例,而是从延迟加载的模块获取实例。我不认为提供商需要被导出。我认为这只适用于指令、组件和管道。我对这个话题不太确定,因为我使用的是Dart,没有
NgModule
s