我试图注入的@Injectable AppState
作为单:
@Injectable()
export class AppState {
public user: string;
constructor() {
}
...
我已经把它放在AppModule
为provider
:
// Application wide providers
const APP_PROVIDERS = [
AppState
];
@NgModule({
bootstrap: [ App ],
declarations: [
App,
ErrorComponent
],
imports: [ // import Angular's modules
BrowserModule,
FormsModule,
HttpModule,
RouterModule.forRoot(ROUTES, { useHash: true })
],
providers: [
APP_PROVIDERS
]
})
所以,我刚刚创建了"submodule"
:
@NgModule({
//...
providers: [ UsersService, AppState ]
})
export default class LoginModule {
在LoginComponent
我做的:
export class Login implements OnInit {
constructor(private appState: AppState) {}
public doLogin():void {
this.appState.user = this.form.value.mail;
}
}
然而,在另一个组件命名Profile
,似乎this.appState.user
是不确定的。
@NgModule({
//...
providers: [ AppState ]
})
export default class ProfileModule { /... }
@Component({
//...
})
export class Profile implements OnInit {
constructor(private appState: AppState) {
}
ngOnInit():void {
this.user = this.appState.user; <<<<<<<<<<<<< it's undefined
}
}
为什么this.appState.user
在另一个组件undifined如果I'set之前?