Shared Variable Used Globally in Angular 6 Components

荒凉一梦 提交于 2019-12-25 03:38:27

问题


I am working on Angular6 todo list task app. I have tasks categories when click on category I get this category's tasks it working with me well but I need to set categories in nav-component like image below. so I need store selected category id in value to use it in todo-component any help ..?


回答1:


I found a way to make a shareable Object :

create new service called GlobaleVariable.ts and write this on it :

@Injectable()
export class GlobalVariblesClass {    
 sharedCategory$ = new ReplaySubject<Groups>(1);

 updateCategoryValue(g: Groups) {

      return this.sharedCategory$.next(g);
   }
}

then you can inject this class in any Component Constructor to use it :)



来源:https://stackoverflow.com/questions/53457721/shared-variable-used-globally-in-angular-6-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!