作者:爱你不愿放cwy | 来源:互联网 | 2022-12-09 17:43
我正在尝试使用存储经过身份验证的用户详细信息的应用程序范围的服务(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() {
}
}
用户配置文件,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() {
}
}
eduPeeth..
17
正如您已将TestService声明为 -
@Injectable({
providedIn: 'root'
})
这意味着您要添加到AppRoot模块.
无需明确添加CoreModule
,因此从提供者中删除CoreModule
.删除以下 -
providers: [
TestService
]
正如你所添加的TestSevice
中CoreModule
这是在已经加入RootModule
这就是它之所以constructor
获得多次调用.
所以使用上面的任何一个.
1> eduPeeth..:
正如您已将TestService声明为 -
@Injectable({
providedIn: 'root'
})
这意味着您要添加到AppRoot模块.
无需明确添加CoreModule
,因此从提供者中删除CoreModule
.删除以下 -
providers: [
TestService
]
正如你所添加的TestSevice
中CoreModule
这是在已经加入RootModule
这就是它之所以constructor
获得多次调用.
所以使用上面的任何一个.
我只需要评论...我在互联网上寻找了一个解决方案,以了解为什么我同时调用了ts和js文件而我的代码中没有保留单例服务。阅读@eduPeeth的帖子后,搜索了我的代码并找到了对.js文件的引用。花费了这么多愤怒的时光。。。