简短版:第一个链接中的自定义 Web 组件示例对我不起作用。为什么不?我在 Dartium 中运行它。
长版:我将本教程中的这个Dart Web UI 示例
复制并粘贴到 Dart 编辑器中并尝试运行它。页面上什么都没有显示。我以前使用过 dart,但没有使用 web 组件,所以我注意到这段代码与我编写的其他代码之间的一个区别是没有,所以我在底部添加了它。现在我得到了错误:<script src="packages/browser/dart.js"></script>
内部错误:Dart_Invoke:没有找到顶级函数'main'。
我print
在函数里放了一条语句,main()
在报错之前就打印了文字,很奇怪。我猜测并尝试在自定义组件中main() {}
的标签内添加。<script>
也就是说,脚本标签看起来像:
<script type="application/dart">
import 'package:web_ui/web_ui.dart';
main() {print("in main in component");}
class CounterComponent extends WebComponent {
int count = 0;
void increment() { count++; }
}
</script>
现在该错误消失了,并且打印了两个打印语句,但没有任何反应。
为了您的方便,这里是原始教程代码:
<!DOCTYPE html>
<!--
Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
for details. All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE file.
-->
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link rel="stylesheet"
href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.1/css/bootstrap.css">
</head>
<body>
<element name="click-counter" constructor="CounterComponent" extends="div">
<template>
<button on-click="increment()">Click me</button>
<span>(click count: {{count}})</span>
</template>
<script type="application/dart">
import 'package:web_ui/web_ui.dart';
class CounterComponent extends WebComponent {
int count = 0;
void increment() { count++; }
}
</script>
</element>
<div class="well">
<div is="click-counter"></div>
</div>
<script type="application/dart">
main(){}
</script>
</body>
</html>