打字稿错误:“菜单项|类型”上不存在属性“项目” MenuItem []'

时间:2018-11-01 12:42:54

标签: angular typescript menu primeng

我正在尝试使用PrimeNG创建一个嵌套菜单。下面的代码给出了编译错误:

export class AppComponent {
    title = 'test-proj';
    items: MenuItem[];
    ngOnInit() {
        this.items = [
            {
                label: 'File',
                icon: 'pi pi-pw pi-file',

            },
            {
                label: 'Regions',
                icon: 'pi pi-fw pi-pencil',
                items: [] as MenuItem[]
            },
            {
                label: 'Help',
                icon: 'pi pi-fw pi-question',
            }
        ];

        for(let i = 0; i< 5; i++){
          this.items[1].items[i] = {
            label: 'R'+i,
            items: [] as MenuItem[]
          }
          console.log(this.items[1].items[i]);
          for(let j = 0; j< 4; j++){
            this.items[1].items[i].items[j] = {// error at this line
              label: 'A'+j, 
              items: [] as MenuItem[]
            }


          }
        }

    }
}

错误:

ERROR in app.component.ts(41,36): error TS2339: Property 'items' does not exist on type 'MenuItem | MenuItem[]'.
  Property 'items' does not exist on type 'MenuItem[]'.

package.json:

"dependencies": {
    "@angular/animations": "~7.0.0",
    "@angular/common": "~7.0.0",
    "@angular/compiler": "~7.0.0",
    "@angular/core": "~7.0.0",
    "@angular/forms": "~7.0.0",
    "@angular/http": "~7.0.0",
    "@angular/platform-browser": "~7.0.0",
    "@angular/platform-browser-dynamic": "~7.0.0",
    "@angular/router": "~7.0.0",
    "core-js": "^2.5.4",
    "font-awesome": "^4.7.0",
    "primeicons": "^1.0.0",
    "primeng": "^7.0.0-beta.1",
    "rxjs": "~6.3.3",
    "zone.js": "~0.8.26"
}

一种解决方法是使用 any 作为项目的类型。我来自Java背景,无法理解为什么会出现此错误。有没有办法解决此问题,将项目类型保持为MenuItem?

编辑1:日志

logs

1 个答案:

答案 0 :(得分:1)

尝试

binompdf