我正在尝试使用存储已验证用户详细信息的应用程序范围服务(UserService)。我已经设置了一些路由,但发现每个路由都实例化了UserService。我希望他们共享相同的UserService。
我创建了一个包含TestService作为提供程序的CoreModule,并将其导入到AppModule中。
core.module.ts:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { TestService } from '../test.service';
@NgModule({
imports: [
CommonModule
],
declarations: [],
providers: [
TestService
]
})
export class CoreModule { }
test.service.ts:
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class TestService {
constructor() { console.log('testService constructor called');}
}
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AdminComponent } from './layout/admin/admin.component';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { CoreModule } from './core/core.module';
@NgModule({
declarations: [
AppComponent,
AdminComponent,
],
imports: [
BrowserModule,
AppRoutingModule,
FormsModule,
CoreModule
],
providers: [
],
bootstrap: [AppComponent]
})
export class AppModule { }
app-routing.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Routes, RouterModule } from '@angular/router';
import { BasicLoginComponent } from './basic-login/basic-login.component';
import { HttpClientModule } from '@angular/common/http';
import { AdminComponent } from './layout/admin/admin.component';
const routes: Routes = [
{
path: '',
component: AdminComponent,
children: [
{
path: 'home',
loadChildren: './dashboard/dashboard.module#DashboardModule'
},
{
path: 'user/profile',
loadChildren: './user-profile/user-profile.module#UserProfileModule'
}
]
},
]
@NgModule({
imports: [
CommonModule,
RouterModule.forRoot(routes),
HttpClientModule
],
exports: [
[RouterModule]
],
declarations: []
})
export class AppRoutingModule { }
我已经将TestService注入DashboardComponent和UserProfileComponent构造函数中。但是,当在其中两个组件之间路由时,两次调用TestService构造函数。
这似乎很简单,但是我却无法正确解决。谁能为我指出正确的方向进行故障排除?
*编辑
dashboard.component.ts
import {AfterViewInit, Component, OnInit, ViewEncapsulation} from '@angular/core';
/*import {NotificationsService} from 'angular2-notifications';*/
import { UserService } from '../user.service.js';
import { LocalStorageService } from '../../../node_modules/ngx-webstorage';
import { TestService } from '../test.service.js';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.scss'],
encapsulation: ViewEncapsulation.None
})
export class DashboardComponent implements OnInit, AfterViewInit {
constructor(private userService:UserService, private localSt:LocalStorageService,
private testService:TestService) { // private servicePNotify: NotificationsService
}
ngOnInit() {
}
}
user-profile-component.ts:
import {Component, OnInit} from '@angular/core';
import {animate, style, transition, trigger} from '@angular/animations';
import {Http} from '@angular/http';
import { TestService } from '../test.service';
@Component({
selector: 'app-user-profile',
templateUrl: './user-profile.component.html',
styleUrls: [
'./user-profile.component.scss',
'../../assets/icon/icofont/css/icofont.scss'
],
})
export class UserProfileComponent implements OnInit {
constructor(public http: Http, private userService: UserService,
private testService:TestService) {
}
ngOnInit() {
}
}
答案 0 :(得分:10)
您已将TestService声明为-
@Injectable({
providedIn: 'root'
})
这意味着您要添加到AppRoot模块。
无需在CoreModule
中显式添加,因此从CoreModule
的提供者中删除。删除以下内容-
providers: [
TestService
]
在TestSevice
中添加CoreModule
中的RootModule
时,就是constructor
被多次调用的原因。
因此,请使用上方的任意一个。
答案 1 :(得分:0)
@Injectable({
providedIn: 'root'
})
providedIn
选项的root
意味着该服务在整个应用程序中都可以作为singleton
服务使用。No provider for xxxservice
的{{1}} bcz这样的错误。注意:请确保使用该概念以获得更好的结果。