【问题标题】:primeng MenuItem: Property 'command' does not exist on type 'never'primeng MenuItem:“从不”类型上不存在属性“命令”
【发布时间】:2021-06-23 19:59:58
【问题描述】:

我想实现一个使用Menu Model API 的primeng ContextMenu。 MenuItem 对象有一个名为“command”的属性,据我了解,它是一个函数。

由于我的后端通过 HTTP 调用发送可用的上下文菜单项,因此我需要在接收数据时在前端分配此功能。

代码按预期编译和工作,但是我的 IDE 显示错误:

    ERROR in src/app/protected/workbench/sidebar/sidebar.component.ts:61:21 - error TS2339: Property 'command' does not exist on type 'never'.

61           ctxAction.command = this.execCtxAction;

ctxAction 是 MenuItem 类型,当我将鼠标悬停在变量上时确认。

分配如下:

public getFavourites() {
  let url = "http://localhost:8081/api/fav/all";
  this.http.get<Fav[]>(url).subscribe(
    res => {
      for (const resItem of res) {
        let ctxActionList = resItem.dtoEntity.ctxActionList;
        // the variable ctxAction is of type MenuItem[]
        for (let ctxAction of ctxActionList) {
          ctxAction.command = this.execCtxAction;
        }
      }
      this.fav = res;
    },
    err => { alert("there is an error") }
  );
}

execCtxAction(): void {
  console.log('execute context action');
}

我在这里做错了什么?

提前非常感谢!

【问题讨论】:

    标签: angular typescript contextmenu primeng


    【解决方案1】:

    发现问题。虽然 IDE 识别出隐式类型,但编译器并没有(或类似的东西)。

    因此,通过在将上下文操作列表分配给相应变量的过程中包含特定类型定义来解决此问题。 或者换句话说,改变这一行:

    let ctxActionList = resItem.dtoEntity.ctxActionList;
    

    进入这个:

    let ctxActionList: MenuItem[] = resItem.dtoEntity.ctxActionList;
    

    【讨论】:

      猜你喜欢
      • 2018-04-15
      • 2013-02-11
      • 2021-05-02
      • 1970-01-01
      • 2017-10-24
      • 2021-12-30
      • 2017-04-09
      • 2018-03-09
      • 2021-12-03
      相关资源
      最近更新 更多