我试图按照这里找到的代码:
component_created_in_code_test.html
component_created_in_code.dart
但是,当我获取依赖项并在 dartium 中运行代码时,出现以下错误。调用 ComponentItem 的 create() 方法时会出现此错误(在 .dart 代码中):
Breaking on exception: Class 'SayHello' has no instance method 'created_autogenerated'.
我在下面稍微重写了它们(代码是相同的,除了 main 已被移动为 dart 代码而不是内联):
<!-- component_created_in_code_test.html -->
<!doctype html>
<!--
Copyright (c) 2013, 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">
<script src="packages/web_ui/testing/testing.js"></script>
</head>
<body>
<element name="say-hello">
<template>Hello {{name}}!</template>
<script type='application/dart' src="component_created_in_code.dart">
</script>
</element>
<say-hello name="component create in html"></say-hello>
</body>
</html>
和以下飞镖代码,
//component_created_in_code.dart
library component_created_in_code;
import 'dart:async';
import 'dart:html';
import 'package:web_ui/web_ui.dart';
class SayHello extends WebComponent {
String name;
}
void main() {
Timer.run(() {
var hello = new SayHello()
..host = new DivElement()
..name = 'component created in code';
// "hello" is the DOM node.
// "hello.xtag" is your SayHello object.
// We are working on making these be the same object.
// If the component uses data-binding, we need to make sure the
// "lifecycle" methods get called. We are working to make this be
// automatic too.
var lifecycleCaller = new ComponentItem(hello)..create();
document.body.nodes.add(hello.host);
lifecycleCaller.insert();
window.postMessage('done', '*');
});
}
看来这个 dart-lang 示例有问题。我是否遗漏了什么或者代码只是无聊?
在得到这个问题的回答后,我打包了这个问题的工作解决方案。
只需从 git 中提取,然后导入 dartEditor。然后从编辑器中“pub install”和“reanalyze source”(从不伤害),然后右键单击“web/component_created_in_code.html”上的“Run in Dartium”。