索引.html
<!doctype html>
<html>
<head>
</head>
<script>
var Apple = function(type) {
this.type = type;
this.color = "red";
};
Apple.prototype.getInfo = function() {
return this.color + ' ' + this.type + ' apple';
};
</script>
<body>
<script type="application/dart" src="index.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>
索引.dart
import 'dart:js' as js;
import 'dart:html' as dom;
import 'package:js/js.dart';
main() {
// this works fine
var apple = new js.JsObject(js.context['Apple'], ['Macintosh']);
print(apple.callMethod('getInfo', []));
print(new Apple().getInfo());
}
@Js() // about to being changed to @JS
class Apple {
external String get type;
external set type(String type);
external String get color;
external set color(String color);
external factory Apple(String type);
}
只需添加@Js()
注释即可
例外:'dart:js':断言失败:第 393 行:'p.isNamed' 不正确。天文台在http://127.0.0.1:35293/监听 内部错误:Dart_Invoke 期望加载库参数“目标”。
更新
删除external factory Apple(String type);
修复了异常。
现在我明白了
天文台收听http://127.0.0.1:38029/
red Macintosh apple
异常:“Apple”类没有实例方法“getInfo”。NoSuchMethodError:找不到方法:“getInfo”
接收者:“Apple”实例
参数:[...]
Apple.getInfo
main