Angular 5 属性 'submenu' 类型不存在

Angular 5 Property 'submenu' does not exist on type

我刚刚创建了生成 "Menu" 的服务,我过去常常从组件调用方法,我添加了更多菜单,这是工作流程。

这里menu.service.ts文件

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { Api } from '../_providers/api/api';

@Injectable()
export class MenuService {

    menuLinks = [
        {
            label: "Dashboard",
            details: "12 New Updates",
            routerLink: "dashboard",
            iconType: "pg",
            iconName: "home",
            thumbNailClass: "bg-success"
        },
        {
            label: "Email",
            details: "234 New Emails",
            routerLink: "email/list",
            iconType: "pg",
            iconName: "mail"
        },
         {
            label: "Classroom",
            iconType: "pg",
            iconName: "laptop",
            toggle: "close",
            submenu: [
                {
                    label: "Classroom UI Test",
                    routerLink: "classroom/class",
                    iconType: "letter",
                    iconName: "AC",
                },
                {
                    label: "Grades",
                    routerLink: "classroom/grade-list",
                    iconType: "fa",
                    iconName: "graduation-cap",
                }
            ]
        }]


    private menuList = new BehaviorSubject(this.menuLinks)
    currentMenuList = this.menuList.asObservable();

    constructor(public Api: Api) {
        this.getAllCourse();
    }


    addCourseinMenu(courseInfo) {
        console.log("adding new course");
        this.menuLinks.forEach(function (menuObj) {
            if (menuObj.label === "Classroom") {
                menuObj.submenu.push({
                    label: courseInfo.shortName,
                    routerLink: "classroom/" + courseInfo.courseId,
                    iconType: "letter",
                    iconName: courseInfo.name.slice(0, 2).toUpperCase(),
                })
            }
        })
        this.menuList.next(this.menuLinks);
    }

    getAllCourse() {
        let that = this;
        console.log("checking all course");
        this.Api.getAll('getAllCouseNameandId').subscribe((response) => {
            const courseInfo: any = response;
            courseInfo.forEach(function (courseObj) {
                let name = courseObj.name;
                that.menuLinks.forEach(function (menuObj) {
                    if (menuObj.label === "Classroom") {
                        menuObj.submenu.push({       //Error Line
                            label: courseObj.shortName,
                            routerLink: "classroom/" + courseObj._id,
                            iconType: "letter",
                            iconName: name.slice(0, 2).toUpperCase(),
                        })
                    }
                })
            })
        })
    }
}

当我 运行 我得到这个错误

编译失败

src/app/_services/menu.service.ts(400,25):错误 TS2339:属性 'submenu' 在类型 '{ label: string; 上不存在;详细信息:字符串;路由器链接:字符串;图标类型:字符串;图标名称:字符串; thumbNa...'. 属性 'submenu' 不存在于类型 '{ label: string;详细信息:字符串;路由器链接:字符串;图标类型:字符串;图标名称:字符串; thumbNa...'. src/app/_services/menu.service.ts(420,33):错误 TS2339:属性 'submenu' 在类型 '{ label: string; 上不存在;详细信息:字符串;路由器链接:字符串;图标类型:字符串;图标名称:字符串; thumbNa...'. 属性 'submenu' 不存在于类型 '{ label: string;详细信息:字符串;路由器链接:字符串;图标类型:字符串;图标名称:字符串; thumbNa...'.

为此使用胖数组 => 而不是 function

  getAllCourse() {
    let that = this;
    console.log("checking all course");
    this.Api.getAll('getAllCouseNameandId').subscribe((response) => {
      const courseInfo: any = response;
      courseInfo.forEach((courseObj:any) {
        let name = courseObj.name;
        this.menuLinks.forEach((menuObj:any) {
          if (menuObj.label === "Classroom") {
            menuObj.submenu.push({       //Error Line
              label: courseObj.shortName,
              routerLink: "classroom/" + courseObj._id,
              iconType: "letter",
              iconName: name.slice(0, 2).toUpperCase(),
            })
          }
        })
      })
    })
  }