Angular 2:简单 *ngFor 不起作用

Cha*_*har 0 angular-ng angular2-databinding angular

我的里面有这个app/component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <ul>
      <li *ngFor="let course of courses">
        {{ course }}
      </li>
    </ul>
  `,
  styleUrls: ['./app.component.css']
})

export class AppComponent {
  course = ['Course 1', 'Course 2', 'Course 3'];
}
Run Code Online (Sandbox Code Playgroud)

在我的 app.module.ts 中,我还导入了必要的东西

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common'

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    CommonModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

这只是一个简单的 *ngFor 代码,但为什么它不起作用。除了这些我没碰过任何东西

Jot*_*edo 5

您有一个拼写错误,请检查您的组件定义。您已定义但在模板中course = [...];使用。courses尝试将组件中的变量名称更改为 course