英文:
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:
<a class="nav-link" [routerLink]="linkEnabled ? ['/libraries']: null" [routerLinkActive]="linkEnabled ? 'active-route' : 'is-disabled'">
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 '../nav-sidebar/nav-sidebar.component';
and added it to the constructor:
constructor(private store: Store<AppState>,
..........
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 => {
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<boolean> {
console.log("activate routes called");
return of(this.linkEnabled = true);
}
and then in the ngOnit do something like:
this.activateRoutes().subscribe((link) => {
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 => {
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]="linkEnabled ? 'active-route' : 'is-disabled'"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论