我有一个具有数组的组件,我想把这个对象传递给所有其他组件。在我的组件中,我有一个函数根据Cookie值获取数据,然后我想将对象数据放到其他组件{{lang.data1}}中。
this.lang=this._webService.getlang(this.cookieValue);如何在所有其他组件中使用lang。
发布于 2017-06-18 18:35:08
您可以使用dependency injection和注入语言服务,也可以使用BaseComponent
export class BaseComponent {
public language: any;
constructor() {
this.language = configuration.language;
}
}
export class BlockComponent extends BaseComponent {
constructor() {
super();
console.log(this.language);
}
}发布于 2017-06-18 23:57:26
使用具有数组的组件作为服务。在根组件中初始化该服务,并在所需任何组件处导入该服务数组。
发布于 2017-06-19 00:03:07
如果这是针对中小型应用程序
然后创建服务,以便使用事件发射器将信息传递给所有组件
但是,如果这是一个大型应用程序,我建议您使用ngrx i:e redux方法,其中所有数据都存储在特定位置。
如果你想检查redux是如何工作的,请看git hub中的这个repo,里面有一个关于ngrx store和angular的非常简单的演示。
https://stackoverflow.com/questions/44614018
复制相似问题