首页 > 解决方案 > 订阅时不调用作为参数传递的函数

问题描述

我正在生成一个节点,我在其中传递函数:

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.

标签: angularfunctionobjectsubscription

解决方案


我以不同的方式解决了我的问题。我什至不需要在订阅时执行函数,我只是传递了这样的值:

function : () => this.myFunction()

并且值会随着函数的返回值改变而改变。所以我猜这就像订阅函数的值一样。


推荐阅读