【发布时间】:2019-11-28 17:04:52
【问题描述】:
我的 Angular 7 项目中有很多组件。厌倦了在组件之间共享一个值但无法正常工作。我在下面给出了我的代码。任何知道方法的人请帮助找到解决方案。
文件夹: 我的组件:
app ->
->power
->air
->pollution
->wind
power.component.ts:
@Output() public globalValue= new EventEmitter();
ngOnInit(){
this.globalValue.emit('helloworld');
}
air.component.ts:
@Input() globalValue: string;
ngOnInit(){
console.log(this.globalValue); //Output should be helloworld
}
pollution.component.ts:
@Input() globalValue: string;
ngOnInit(){
console.log(this.globalValue); //Output should be helloworld
}
wind.component.ts:
@Input() globalValue: string;
ngOnInit(){
console.log(this.globalValue); //Output should be helloworld
}
【问题讨论】:
-
不要使用输入或输出,它只在父子组件之间有效。只需在服务上设置变量并将服务注入组件的构造函数即可。
标签: angular6 angular7 angular8