简短版本:第一个链接中的自定义Web组件示例不适用于我。为什么不?我在Dartium中运行它。
完整版:
我将this Dart Web UI示例从this tutorial复制并粘贴到Dart编辑器中,并尝试运行它。页面上没有任何内容。我以前使用过dart,但没有使用过Web组件,因此我注意到此代码与我编写的其他代码之间的区别是没有<script src="packages/browser/dart.js"></script>
,因此我在底部添加了它。现在我得到了错误:
Internal error: Dart_Invoke: did not find top-level function 'main'.
I put a print
statement in the main()
function, and the text was printed before the error, which is strange. I guessed and tried adding main() {}
inside the <script>
tag that was in the custom component. That is, the script tag looked like:
<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>
最佳答案
Web UI应用程序需要“构建”(通常由项目根目录中称为build.dart
的脚本,see this article了解更多详细信息)。
如果我转到Dart编辑器,则使用向导创建一个新的测试项目,然后选择 Web项目(使用web_ui库)选项,这将创建包括构建脚本的样板。
打开html文件,然后粘贴github教程中的代码,替换掉已有的代码。保存文件时,将调用build.dart
,将构建版本输出到/ web / out /
点击运行按钮,Dart编辑器将在Dartium中打开该应用程序(它知道在URL中添加/out/
)。
关于dart - 简单的Dart Web组件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17158809/