angular - 订阅时不调用作为参数传递的函数
问题描述
我正在生成一个节点,我在其中传递函数:
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()
用户更改时将触发的。当用户更改时,我想再次触发显示功能以返回 true 或 false。
下面是函数 displayApkStoreMenu 的示例:
displayApkStoreMenu(): boolean {
// some code...
return // either true or false
}
现在,当用户更改时,根本不会调用该函数,并且 display 的值也不会更改。但我知道订阅正在触发,因为当我放置console.log
而不是node.display = display()
它时会触发console.log
.
解决方案
我以不同的方式解决了我的问题。我什至不需要在订阅时执行函数,我只是传递了这样的值:
function : () => this.myFunction()
并且值会随着函数的返回值改变而改变。所以我猜这就像订阅函数的值一样。
推荐阅读
- javascript - 转换成word文档时如何保持html页面样式?
- javascript - 如何隐藏第一张图片的左箭头和最后一张图片的右箭头
- spring-boot - 扩展在百里香叶中无法正常工作
- plsql - 单行 SQL 输出
- flutter - 有没有办法使用颤振访问 twitter API 而不会出错?
- node.js - 即使我的命令几乎完全相同 2 个命令可以工作,而其他 2 个命令不 discord.js
- android - 您好,我正在尝试运行一个简单的 Web 视图,但我被卡住了
- firebase - 如果我的 Cloud Firestore 的 Spark 计划超过了限制并且我没有升级我的计划会怎样?
- haskell - 依赖包编译错误,需要语言扩展
- javascript - 在反应组件内运行lib回调函数