如何修复Dart中的“未为类定义getter”错误?

时间:2019-06-23 06:49:28

标签: angular dart angular-dart

我的Angular Dart应用无法编译。我是Angular Dart的新手,我不明白问题是什么。

这是BookListComponent:

import 'package:angular/angular.dart';

import '../book.dart';

@Component(
  selector: 'book-list',
  styleUrls: ['book_list_component.css'],
  templateUrl: 'book_list_component.html',
  directives: [NgFor],
)
class BookListComponent {
  List<Book> books = [
    new Book('Test 1'),
    new Book('Test 2'),
    new Book('Test 3')
  ];
}

这是BookListComponent模板:

<ul>
  <li ngFor="let book of books">{{ book.title }}</li>
</ul>

这是Book类

class Book {
  String title;

  Book(this.title);
}

编译时发生此错误:[error] The getter 'book' isn't defined for the class 'BookListComponent'. (package:bookshelf/src/book_list/book_list_component.template.dart)

1 个答案:

答案 0 :(得分:1)

模板错误,ngFor缺少前导星。

<ul>
  <li *ngFor="let book of books">{{ book.title }}</li>
</ul>

还如下更改Book类。

class Book {
  String title;
  Book(String title) {
    this.title = title;  
  }
}