【问题标题】:AngularDart tour of heroes tutorial throws an error 'Hero' isn't a function?AngularDart 英雄之旅教程抛出错误“英雄”不是函数?
【发布时间】:2018-07-17 19:30:25
【问题描述】:

错误:“英雄”不是函数。 (invocation_of_non_function at [angular_dart_tour_of_heroes] lib\app_component.dart:18)

hero.dart

class Hero {
  final int id;
  String name;

  Hero(this.id, this.name);
}

app_component.dart

import 'package:angular/angular.dart';
import 'package:angular_components/angular_components.dart';

import 'hero.dart';

@Component(
  selector: 'my-app',
  styleUrls: const ['app_component.css'],
  templateUrl: 'app_component.html',
  directives: const [materialDirectives,],
  providers: const [materialProviders],
)
class AppComponent {
  final title = 'Tour of Heroes';
  Hero hero = Hero(1, 'Windstorm');

}

app_component.html

<h1>{{title}}</h1>
<h2>{{hero.name}}</h2>
<div><label>id: </label>{{hero.id}}</div>
<div><label>name: </label>{{hero.name}}</div>

【问题讨论】:

    标签: dart angular-dart


    【解决方案1】:

    听起来您使用的是太旧的 Dart 版本,仍然需要现在可选的 new

    【讨论】:

    • Dart VM 版本:“windows_x64”上的 1.24.3(2017 年 12 月 13 日星期三 18:05:29)
    • Angular 5 需要 Dart 2。webdev.dartlang.org/tools/sdk#install 展示了如何安装 Dart 2 开发频道(还没有最终版本)。
    【解决方案2】:

    您似乎使用的是 Dart 1 下教程的 Angular 5 / Dart 2 版本。您使用的是哪个版本的 Dart SDK? (运行:dart --version)。

    我建议使用 Angular 2 和 Dart 2

    如果你想使用 Dart 1.24.3,那么你需要Angular 4 version of the tutorial

    【讨论】:

    • Dart VM 版本:“windows_x64”上的 1.24.3(2017 年 12 月 13 日星期三 18:05:29)
    • 我已经编辑了我的答案,以提供指向本教程 Angular 4 版本的链接。如果您想继续使用 Dart 1.24.3,请使用该版本的应用程序源。但我建议你使用 Angular 2 和 Dart 2。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 2017-03-26
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    相关资源
    最近更新 更多