dai*_*a99 6 typescript angular-services angular-module angular angular-router
我正在尝试使用存储经过身份验证的用户详细信息的应用程序范围的服务(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 { }
Run Code Online (Sandbox Code Playgroud)
test.service.ts:
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class TestService {
constructor() { console.log('testService constructor called');}
}
Run Code Online (Sandbox Code Playgroud)
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 { }
Run Code Online (Sandbox Code Playgroud)
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 { }
Run Code Online (Sandbox Code Playgroud)
我已将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() {
}
}
Run Code Online (Sandbox Code Playgroud)
用户配置文件,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() {
}
}
Run Code Online (Sandbox Code Playgroud)
edu*_*eth 17
正如您已将TestService声明为 -
@Injectable({
providedIn: 'root'
})
Run Code Online (Sandbox Code Playgroud)
这意味着您要添加到AppRoot模块.
无需明确添加CoreModule,因此从提供者中删除CoreModule.删除以下 -
providers: [
TestService
]
Run Code Online (Sandbox Code Playgroud)
正如你所添加的TestSevice中CoreModule这是在已经加入RootModule这就是它之所以constructor获得多次调用.
所以使用上面的任何一个.
| 归档时间: |
|
| 查看次数: |
4589 次 |
| 最近记录: |