订阅时不调用作为参数传递的函数
Passed function as parameter isn't called on subscribe
我正在生成一个节点,我在其中传递函数:
this.navigatinoService.generateNode(null, null... , this.navigationService.displayApkStoreMenu,..)
生成函数看起来像这样。
public generateNode(translationId: string, iconId: string, display: any, disabled, routerLink: string, children: SideNavNode[]): SideNavNode {
const node: SideNavNode = {
translationId: translationId,
iconId: iconId,
display: this.userService.getUser().subscribe(() => node.display = display()),
disabled: disabled,
routerLink: routerLink,
children: children
};
this.SIDENAV_TREE_DATA.push(node);
return node;
}
我不想订阅将在用户更改时触发的 userService.getUser()
。当用户更改时,我想再次触发显示功能 return true 或 false。
这里是函数 displayApkStoreMenu 的一个例子:
displayApkStoreMenu(): boolean {
// some code...
return // either true or false
}
现在,当用户更改时,根本不会调用函数,显示值也不会更改。但我知道订阅正在触发,因为当我输入 console.log
而不是 node.display = display()
时,它会触发 console.log
.
我用不同的方法解决了我的问题。我什至不需要在订阅时执行函数,我只是传递了这样的值:
function : () => this.myFunction()
并且当函数的return值改变时,该值也会改变。
所以我猜这就像订阅函数的值一样。
我正在生成一个节点,我在其中传递函数:
this.navigatinoService.generateNode(null, null... , this.navigationService.displayApkStoreMenu,..)
生成函数看起来像这样。
public generateNode(translationId: string, iconId: string, display: any, disabled, routerLink: string, children: SideNavNode[]): SideNavNode {
const node: SideNavNode = {
translationId: translationId,
iconId: iconId,
display: this.userService.getUser().subscribe(() => node.display = display()),
disabled: disabled,
routerLink: routerLink,
children: children
};
this.SIDENAV_TREE_DATA.push(node);
return node;
}
我不想订阅将在用户更改时触发的 userService.getUser()
。当用户更改时,我想再次触发显示功能 return true 或 false。
这里是函数 displayApkStoreMenu 的一个例子:
displayApkStoreMenu(): boolean {
// some code...
return // either true or false
}
现在,当用户更改时,根本不会调用函数,显示值也不会更改。但我知道订阅正在触发,因为当我输入 console.log
而不是 node.display = display()
时,它会触发 console.log
.
我用不同的方法解决了我的问题。我什至不需要在订阅时执行函数,我只是传递了这样的值:
function : () => this.myFunction()
并且当函数的return值改变时,该值也会改变。 所以我猜这就像订阅函数的值一样。