Her*_*aya 1 typescript angular
美好的一天.我是使用VSCode的Type Script的新手.
获得以下错误:
error TS2345: Argument of type 'Menu' is not assignable to parameter of type '{ state: string; name: string; type: string; icon: string;
badge?: undefined; children?: undefine...'
Run Code Online (Sandbox Code Playgroud)
该代码:
import { Injectable } from '@angular/core';
export interface BadgeItem {
type: string;
value: string;
}
export interface ChildrenItems {
state: string;
name: string;
type?: string;
}
export interface Menu {
state: string;
name: string;
type: string;
icon: string;
badge?: BadgeItem[];
children?: ChildrenItems[];
}
const MENUITEMS = [
{
state: '/',
name: 'HOME',
type: 'link',
icon: 'explore'
},
{
state: 'account',
name: 'ACCOUNT',
type: 'sub',
icon: 'explore',
badge: [
{type: 'purple', value: 'new'}
],
children: [
{state: 'users', name: 'USERS'},
]
}
];
@Injectable()
export class MenuService {
getAll(): Menu[] {
return MENUITEMS;
}
add(menu: Menu) {
MENUITEMS.push(menu);
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将受到高度赞赏.
只需在MENUITEMS下面指定类似的类型,警告就会消失
const MENUITEMS : Menu[] = [
{
state: '/',
name: 'HOME',
type: 'link',
icon: 'explore'
},
{
state: 'account',
name: 'ACCOUNT',
type: 'sub',
icon: 'explore',
badge: [
{type: 'purple', value: 'new'}
],
children: [
{state: 'users', name: 'USERS'},
]
}
];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1425 次 |
| 最近记录: |