监听ngrx中属性的变化

huangapple go评论46阅读模式
英文:

Listening for change to property in ngrx

问题

在我的第一个组件中,我设置了一个属性,如下所示:

linkEnabled: boolean = false;

当这个属性被设置为false时,某些路由将无法被用户访问,我在HTML文件中设置如下:

<a class="nav-link" [routerLink]="linkEnabled ? ['/libraries']: null" [routerLinkActive]="linkEnabled ? 'active-route' : 'is-disabled'"></a>

直到选择了一个项目之前,它会保持为false,这是在另一个组件中完成的。

在第二个组件中,我像这样导入了第一个组件:

import { NavSidebarComponent } from '../nav-sidebar/nav-sidebar.component';

然后将它添加到构造函数中:

constructor(private store: Store<AppState>,
            ...
            private navSidebarComponent: NavSidebarComponent
            ) { }

ngOnInit中,设置项目后,我调用linkEnabled的值,并在项目名称不为null时将其设置为true

this.projectNameSub = this.store.pipe(select(ProjectSelectors.selectCurrentProjectName))
  .subscribe(projectName => {
    this.projectName = projectName;
    if(this.projectName != null) {
      this.navSidebarComponent.linkEnabled = true;
    }
  });

我遇到的问题是,我不确定如何让第一个组件监听更改,以便知道linkEnabled现在已经被设置为true?目前它只看到它为false,所以我知道我漏掉了一步,但我不确定是什么。是否有一种方式可以订阅该值,以便在第一个组件的ngOnInit中监听它的变化?

我曾考虑在第一个组件中创建一个类似这样的函数:

public activateRoutes(): Observable<boolean> {
  console.log("activate routes called");
  return of(this.linkEnabled = true);
}

然后在ngOnInit中做类似以下的操作:

this.activateRoutes().subscribe((link) => {
  this.linkEnabled = link;
})

然后在第二个组件的ngOnInit中,而不是执行:

this.navSidebarComponent.linkEnabled = true;

我将执行:

this.navSidebarComponent.activateRoutes();

然而,现在的情况是,在页面加载时,linkEnabled被设置为true,但它根本不起作用,因为我需要它工作。

英文:

In my first component I have set up a property like so:

  linkEnabled: boolean = false;

and when this is set to false, certain routes will not be able to be accessed by users which I've set up in my html file like so:

   &lt;a class=&quot;nav-link&quot; [routerLink]=&quot;linkEnabled ? [&#39;/libraries&#39;]: null&quot; [routerLinkActive]=&quot;linkEnabled ? &#39;active-route&#39; : &#39;is-disabled&#39;&quot;&gt;

This is set to false until a project has been selected, this is done in another component

In the second component I've imported the first one like so:

import { NavSidebarComponent } from &#39;../nav-sidebar/nav-sidebar.component&#39;;

and added it to the constructor:

   constructor(private store: Store&lt;AppState&gt;,
              ..........
              private navSidebarComponent: NavSidebarComponent
              ) { }

and in the ngOnit, where the project is set, I call the linkEnabled value and set to true for when project name is not null:

this.projectNameSub = this.store.pipe(select(ProjectSelectors.selectCurrentProjectName))
  .subscribe(projectName =&gt; {
    this.projectName = projectName;
    if(this.projectName !=null) {
      this.navSidebarComponent.linkEnabled = true;
    }
  });

The issue I am having, is that I am not sure how to get the first component to listen to the changes so that it know that linkEnabled has now been set to true? As at the moment it just sees it as false so I know I am missing a step but I'm just not sure what. Is there a way to subscribe to the value so that it can listen to it changing in the ngOnInit in the first component?

I had thought of creating a function like so within the first component:

public activateRoutes(): Observable&lt;boolean&gt; {
console.log(&quot;activate routes called&quot;);
return of(this.linkEnabled = true);
}

and then in the ngOnit do something like:

this.activateRoutes().subscribe((link) =&gt; {
this.linkEnabled = link;
})

and then in the ngOnit in the second component, instead of doing:
this.navSidebarComponent.linkEnabled = true;

I would do: this.navSidebarComponent.activateRoutes();

However all that is happening is that on page load, the linkEnabled is set to true and it's not working at all as I need it to

答案1

得分: 1

解决了这个问题,通过创建新的操作、减速器和选择器文件将其存储在存储中,然后可以这样做:

在第一个组件中:

this.routeEnabledSub = this.store.pipe(select(RouteSelectors.selectRouteEnabled))
  .subscribe(routeEnabled => {
    this.routeEnabled = routeEnabled;
  });

然后在第二个组件中,像这样更新它:

if(this.projectName != null) {
  this.store.dispatch(RouteActions.setRouteActive(
    { routeActive: true }));
} else {
  this.store.dispatch(RouteActions.setRouteActive(
    { routeActive: false }));
}

在 HTML 中检查 linkEnabled 是否设置如下:

[routerLinkActive]="linkEnabled ? 'active-route' : 'is-disabled'"
英文:

Solved this issue by creating new action, reducer and selectors file to store this in the store and could then do:

this.routeEnabledSub = this.store.pipe(select(RouteSelectors.selectRouteEnabled))
.subscribe(routeEnabled =&gt; {
  this.routeEnabled = routeEnabled;
});

in the first component

and then in the second one, update it like so:

    if(this.projectName !=null) {
      this.store.dispatch(RouteActions.setRouteActive(
        { routeActive: true }));
    } else {
      this.store.dispatch(RouteActions.setRouteActive(
        { routeActive: false }));

and in html check for linkEnabled being set like so:

[routerLinkActive]=&quot;linkEnabled ? &#39;active-route&#39; : &#39;is-disabled&#39;&quot;

huangapple
  • 本文由 发表于 2023年2月7日 00:39:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定