我正在尝试从https://angular.io入门教程中学习角度。我在第 4步时陷入困境。显示列表,我在HeroesComponent中出现错误
找不到名称“ HEROES”
以下是我的代码
HeroesComponenet.ts文件
import { Component, OnInit } from '@angular/core';
import { Hero } from '../hero';
import { HEROES } from '../mock-heroes';
@Component({
selector: 'app-heroes',
templateUrl: './heroes.component.html',
styleUrls: ['./heroes.component.css']
})
export class HeroesComponent implements OnInit {
heroes = HEROES; // ==> this is where i get error.
selectedHero: Hero;
constructor() { }
ngOnInit() {
}
onSelect(hero: Hero): void {
this.selectedHero = hero;
}
}
mock-heroes.ts文件
import { Hero } from './hero';
export const HEROES: Hero[] = [
{ id: 11, name: 'Mr. Nice' },
{ id: 12, name: 'Narco' },
{ id: 13, name: 'Bombasto' },
{ id: 14, name: 'Celeritas' },
{ id: 15, name: 'Magneta' },
{ id: 16, name: 'RubberMan' },
{ id: 17, name: 'Dynama' },
{ id: 18, name: 'Dr IQ' },
{ id: 19, name: 'Magma' },
{ id: 20, name: 'Tornado' }
];
我正在使用Angular 6,而IDE是VS CODE
答案 0 :(得分:0)
您不能使用标准导入约定导入const。需要使用import as
import * as hero from '../mock-heroes';
heroes = hero.HEROES; // use the class
答案 1 :(得分:0)
首先,检查您的文件夹结构。您可以在以下位置找到实时示例: https://angular.io/tutorial/toh-pt2#final-code-review