gpt4 book ai didi

angular - 更改根模块之间的共享数据

转载 作者:太空狗 更新时间:2023-10-29 18:26:26 26 4
gpt4 key购买 nike

<分区>

我有两个模块:

第一:

@NgModule({
imports: [
BrowserModule,
FormsModule,

SharedModule.forRoot()
],
declarations: [
FirstComponent
],
bootstrap: [ FirstComponent ]
})

export class AppModule { }

第二个:

@NgModule({
imports: [
BrowserModule,
FormsModule,

SharedModule.forRoot()
],
declarations: [
ScndComponent
],
bootstrap: [ ScndComponent ]
})

export class AppModule { }

并与静态 .forRoot() 共享模块

@NgModule( {} )
export class SharedModule {
static forRoot() {
return {
ngModule : SharedModule,
providers : [DumyService]
}
}
}

我的 DumyServise 有一些属性 'paramd',我想通过模块观察变化

@Injectable()
export class DumyService {
paramd : string;
}

在模块的两个组件(第 1 和第 2)中,我有

private ds: DumyService

在构造函数中。

然后,假设我在第一个模块组件的某个组件中有一些 onclick 方法,它发生了变化

clickToChangeParam() {
this.ds.paramd = 'new value';
}

我想在两个模块中都进行此更改。我该如何处理?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com