简体   繁体   English

类型上不存在 Angular 5 属性“子菜单”

[英]Angular 5 Property 'submenu' does not exist on type

I just created the service to generate the "Menu", I used to call the method from components and i add some more menu this is work flow.我刚刚创建了生成“菜单”的服务,我曾经从组件中调用该方法,并添加了更多菜单,这是工作流程。

Here menu.service.ts file这里 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(),
                        })
                    }
                })
            })
        })
    }
}

When i run i got this error当我运行时出现此错误

Failed to compile.编译失败。

src/app/_services/menu.service.ts(400,25): error TS2339: Property 'submenu' does not exist on type '{ label: string; src/app/_services/menu.service.ts(400,25): 错误 TS2339: 属性 'submenu' 不存在于类型 '{ label: string; details: string;详细信息:字符串; routerLink: string;路由器链接:字符串; iconType: string;图标类型:字符串; iconName: string;图标名称:字符串; thumbNa...'.拇指呐...'. Property 'submenu' does not exist on type '{ label: string;类型 '{ label: string; 上不存在属性 'submenu' details: string;详细信息:字符串; routerLink: string;路由器链接:字符串; iconType: string;图标类型:字符串; iconName: string;图标名称:字符串; thumbNa...'.拇指呐...'. src/app/_services/menu.service.ts(420,33): error TS2339: Property 'submenu' does not exist on type '{ label: string; src/app/_services/menu.service.ts(420,33): 错误 TS2339: 属性 'submenu' 不存在于类型 '{ label: string; details: string;详细信息:字符串; routerLink: string;路由器链接:字符串; iconType: string;图标类型:字符串; iconName: string;图标名称:字符串; thumbNa...'.拇指呐...'. Property 'submenu' does not exist on type '{ label: string;类型 '{ label: string; 上不存在属性 'submenu' details: string;详细信息:字符串; routerLink: string;路由器链接:字符串; iconType: string;图标类型:字符串; iconName: string;图标名称:字符串; thumbNa...'.拇指呐...'.

use fat array => instead of function for this为此使用胖数组=>而不是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(),
            })
          }
        })
      })
    })
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM