无法在Angular2最终版本中将服务用作单例对象

无法在Angular2最终版本中将服务用作单例对象,angular,dependency-injection,singleton,angular2-services,Angular,Dependency Injection,Singleton,Angular2 Services,我拥有并希望在整个应用程序中使用的服务是 import {Injectable} from '@angular/core'; @Injectable() export class UserDetailsService { objUser: User = new User(); testing: string; getData() { return this.testing; } setData(my:string) {

我拥有并希望在整个应用程序中使用的服务是

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

@Injectable()
export class UserDetailsService {
    objUser: User = new User();
    testing: string;
    getData() {
        return this.testing;        
    }
    setData(my:string) {
        debugger;
        this.testing = my;
    }
}
现在我想在我的一个组件中设置变量测试

import { Component } from '@angular/core';
import {UserDetailsService} from 'servicePath';

@Component({
    selector: 'some',
    templateUrl: 'someUrl',
    providers: [UserDetailsService]
})
export class LoginComponent {
constructor(private _Service: UserDetailsService)

}
onClick(){
_Service.set("my Value");
}
现在在另一个组件中,如果我想使用这个现场测试,我将得到未定义的

import { Component } from '@angular/core';
import {UserDetailsService} from 'URL';

@Component({
    selector: 'somee1',
    templateUrl: 'url'
})
export class LandingComponent {
    constructor(private _UserService: UserDetailsService) {
        debugger;
        var test = _UserService.getData();
        debugger;
    }
}
在我的应用程序模块中,我有这个

@NgModule({
    imports: ...,
    declarations:...,
    bootstrap: [AppComponent],
    providers: [HttpService, UserDetailsService]}]
})

从组件的
提供程序中删除
UserDetailsService
,并仅将其保留在
@NgModule()

Angular2DI为每个提供者维护一个实例。如果您多次提供它,就会得到多个实例。如果您在组件上提供它,您将获得该组件在应用程序中出现的任何实例

添加到
NgModule
的提供程序都添加到根作用域,如果服务提供了多次,则只向根作用域添加一个实例


延迟加载的模块有自己的作用域,因此这与前面解释的情况不同。

要创建共享模块:

@NgModule({
  imports: [CommonModule, RouterModule, ReactiveFormsModule ],
  declarations: [ ErrorMessagesComponent ],
  exports: [ CommonModule, ReactiveFormsModule, HttpModule, RouterModule, ]
})

export class SharedModule {
  static forRoot(): ModuleWithProviders {
    return {
      ngModule: SharedModule,
      providers: [ AuthorizationService, LoginService, AuthGuardService ]
    };
  }
}
@NgModule({
    imports: [ SharedModule, routing],
    declarations: [ HomeComponent], 
    bootstrap: [ HomeComponent ],
    providers: [ ]

}) 
创建共享模块:

@NgModule({
  imports: [CommonModule, RouterModule, ReactiveFormsModule ],
  declarations: [ ErrorMessagesComponent ],
  exports: [ CommonModule, ReactiveFormsModule, HttpModule, RouterModule, ]
})

export class SharedModule {
  static forRoot(): ModuleWithProviders {
    return {
      ngModule: SharedModule,
      providers: [ AuthorizationService, LoginService, AuthGuardService ]
    };
  }
}
@NgModule({
    imports: [ SharedModule, routing],
    declarations: [ HomeComponent], 
    bootstrap: [ HomeComponent ],
    providers: [ ]

}) 
在app.module.ts中(注意sharedModule.forRoot()):

然后在使用共享模块的每个模块中:

@NgModule({
  imports: [CommonModule, RouterModule, ReactiveFormsModule ],
  declarations: [ ErrorMessagesComponent ],
  exports: [ CommonModule, ReactiveFormsModule, HttpModule, RouterModule, ]
})

export class SharedModule {
  static forRoot(): ModuleWithProviders {
    return {
      ngModule: SharedModule,
      providers: [ AuthorizationService, LoginService, AuthGuardService ]
    };
  }
}
@NgModule({
    imports: [ SharedModule, routing],
    declarations: [ HomeComponent], 
    bootstrap: [ HomeComponent ],
    providers: [ ]

}) 

我认为,为了在多个组件中使用该服务,您需要一个共享模块。