- 创建service.module.ts
import { NgModule, ModuleWithProviders } from '@angular/core';
import { SomeSharedService } from './global.service';
export {
SomeSharedService
}
@NgModule()
export class ServicesModule {
static forRoot():ModuleWithProviders{
return {
ngModule:ServicesModule,
providers:[SomeSharedService]
}
}
}
- 新建global.service.ts
import { Injectable } from '@angular/core'
import { BehaviorSubject } from 'rxjs';
@Injectable()
export class SomeSharedService {
public globalVar:BehaviorSubject<any[]> = new BehaviorSubject([]);
public pageLimit = 10;
}
- 在header组件中引入服务并获取值
...
import { SomeSharedService } from 'src/app/services/global.service';
.....
constructor(
private someSharedService: So