在Angular 6组件中全局使用的共享变量 [英] Shared Variable Used Globally in Angular 6 Components

查看:627
本文介绍了在Angular 6组件中全局使用的共享变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Angular6 待办事项列表任务应用程序. 在类别上单击时,我有任务类别 我得到了这个类别的任务 它可以很好地与我配合使用,但是我需要在 nav-component 中设置类别,如下图所示. 因此我需要存储所选类别 ID的值,才能在 todo-component 中使用它 任何帮助..?

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 ..?

推荐答案

我找到了制作可共享对象的方法:

创建名为 GlobaleVariable.ts 的新服务 并写在上面:

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);
   }
}

然后您可以注入该类到任何 Component Constructor 中以使用它:)

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

这篇关于在Angular 6组件中全局使用的共享变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆