Angular 尽管使用了 @Injectable({providedIn: 'root' }) 装饰器,但在组件中注入时服务不作为单例
Angular Service not acting as singleton when injected in components despite using @Injectable({providedIn: 'root' }) decorator
我有一个项目,其中包含两个模块(创建项目时 angular cli 生成的默认 AppModule 和路由模块)、我创建的两个组件和注入构造函数的服务每个组件。我希望有相同的服务实例(每次我注入服务时都是单例,因为它有 @Injectable({providedIn: 'root' }) 装饰器)但是每次我都会得到一个全新的服务实例我导航到我的每个组件。下面是我的代码:
app.module.html
\<router-outlet\>\</router-outlet\>
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { TestComponent } from './test/test.component';
import { Test2Component } from './test2/test2.component';
@NgModule({
declarations: [
AppComponent,
TestComponent,
Test2Component
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { TestComponent } from './test/test.component';
import { Test2Component } from './test2/test2.component';
const routes: Routes = [
{ path: 'test1', component: TestComponent },
{ path: 'test2', component: Test2Component }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
test.component.ts
import { Component} from '@angular/core';
import { TestService } from '../test.service';
@Component({
selector: 'app-test',
templateUrl: './test.component.html',
styleUrls: ['./test.component.css']
})
export class TestComponent {
constructor(private _testService: TestService) { }
}
test2.component.ts
import { Component } from '@angular/core';
import { TestService } from '../test.service';
@Component({
selector: 'app-test2',
templateUrl: './test2.component.html',
styleUrls: ['./test2.component.css']
})
export class Test2Component {
constructor(private _testService: TestService) { }
}
test.service.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class TestService {
private date = new Date();
constructor() {
console.log("Inside the service ", this.date);
}
}
像这样在 app.module.ts 的 providers 数组中提供您的测试服务。创建单例。
供应商:[测试服务]
参考下面link:
https://www.w3resource.com/angular/angular-singleton-service.php
您需要在 app.module.ts 中将您的服务注册为提供商。
providers: [{provide: Service}]
从代码上看,你的服务确实是单例的。
将此插入 app.component.html,然后尝试链接:
<nav>
<ul>
<li><a routerLink="child-a">Child A</a></li>
<li><a routerLink="child-b">Child B</a></li>
</ul>
</nav>
我怀疑您试图通过在浏览器导航栏中写入 url 来检查这两个组件,因此您在认为自己正在重新路由时随时重新加载应用程序。可能是这样吗?
我已经创建了路由和单例服务使用的工作示例 here。
可以看到,点击几次后,构造函数在控制台中只有一次运行:
我有一个项目,其中包含两个模块(创建项目时 angular cli 生成的默认 AppModule 和路由模块)、我创建的两个组件和注入构造函数的服务每个组件。我希望有相同的服务实例(每次我注入服务时都是单例,因为它有 @Injectable({providedIn: 'root' }) 装饰器)但是每次我都会得到一个全新的服务实例我导航到我的每个组件。下面是我的代码:
app.module.html
\<router-outlet\>\</router-outlet\>
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { TestComponent } from './test/test.component';
import { Test2Component } from './test2/test2.component';
@NgModule({
declarations: [
AppComponent,
TestComponent,
Test2Component
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { TestComponent } from './test/test.component';
import { Test2Component } from './test2/test2.component';
const routes: Routes = [
{ path: 'test1', component: TestComponent },
{ path: 'test2', component: Test2Component }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
test.component.ts
import { Component} from '@angular/core';
import { TestService } from '../test.service';
@Component({
selector: 'app-test',
templateUrl: './test.component.html',
styleUrls: ['./test.component.css']
})
export class TestComponent {
constructor(private _testService: TestService) { }
}
test2.component.ts
import { Component } from '@angular/core';
import { TestService } from '../test.service';
@Component({
selector: 'app-test2',
templateUrl: './test2.component.html',
styleUrls: ['./test2.component.css']
})
export class Test2Component {
constructor(private _testService: TestService) { }
}
test.service.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class TestService {
private date = new Date();
constructor() {
console.log("Inside the service ", this.date);
}
}
像这样在 app.module.ts 的 providers 数组中提供您的测试服务。创建单例。
供应商:[测试服务]
参考下面link: https://www.w3resource.com/angular/angular-singleton-service.php
您需要在 app.module.ts 中将您的服务注册为提供商。
providers: [{provide: Service}]
从代码上看,你的服务确实是单例的。
将此插入 app.component.html,然后尝试链接:
<nav>
<ul>
<li><a routerLink="child-a">Child A</a></li>
<li><a routerLink="child-b">Child B</a></li>
</ul>
</nav>
我怀疑您试图通过在浏览器导航栏中写入 url 来检查这两个组件,因此您在认为自己正在重新路由时随时重新加载应用程序。可能是这样吗?
我已经创建了路由和单例服务使用的工作示例 here。
可以看到,点击几次后,构造函数在控制台中只有一次运行: