我有两个用Polymer.dart编写的自定义组件,一个是另一个的父组件。我想获得对父组件中子组件的引用,但如果使用类型注释,则会出现异常。另一方面,如果我使用var
关键字来声明变量,它可以很好地工作。
使用类型时得到的错误消息和堆栈跟踪:
未捕获错误:类型"ChildComponent"不是"ChildComponent"的类型"ChildComponent"的子类型。
堆栈跟踪:
#插入0 ParentComponent.inserted(包:types_test_lib/parent_component.dart?13758556554489:9:67)
#1 _注册生命周期插入。(包:custom_element/custom_element.dart:643:21)
#2 _ZoneBase_runInZone(dart:async/zone.dart:82:17)
#3_ZoneBase_runGuarded(dart:async/zone.dart:99:22)
#4_ZoneBase.executeCallbackGuarded(dart:async/zone.dart:62:21)
#5 _RunAsyncZone.runAsync..(dart:async/zone.dart:205:61)
#6个性能MicrotaskCheckpoint(包:observe/src/microtask.dart:36:17)
#7包装Microtask。。(包装:observe/src/microtask.dart:58:35)
#8 runZonedExperimental(dart:async/zone.dart:259:53)
#9运行ZonedExperimental。(飞镖:异步/区域飞镖:256:34)
#10_ZoneBase_runInZone(dart:async/zone.dart:82:17)
#11_ZoneBase_runUnguarded(dart:async/zone.dart:102:22)
#12 runZonedExperimental(dart:async/zone.dart:255:30)
#13包装Microtask。(包装:observe/src/microtask.dart:54:25)
#14 initPolymer(包装:聚合物/聚合物。省道:72:5)
#15 main(…/D:/workspace/dart/types_test/web/types_test.html:7:22)
异常:类型"ChildComponent"不是"ChildComponent"的类型"ChildComponent"的子类型。
index.html:
<!DOCTYPE html>
<html>
<head>
<link rel="import" href="parent_component.html"/>
<link rel="import" href="child_component.html"/>
<script src="packages/polymer/boot.js"></script>
</head>
<body>
<parent-component>
<child-component/>
</parent-component>
<script type="application/dart">
void main() {}
</script>
</body>
</html>
parent_component.html:
<!DOCTYPE html>
<html>
<body>
<polymer-element name="parent-component" extends="div">
<template></template>
<script type="application/dart" src="parent_component.dart"></script>
</polymer-element>
</body>
</html>
parent_component.dart:
import "package:polymer/polymer.dart";
import "child_component.dart";
@CustomTag("parent-component")
class ParentComponent extends PolymerElement with ObservableMixin {
void inserted() {
var childComponent = host.query("child-component").xtag;
// !!! ChildComponent childComponent = host.query("child-component").xtag;
}
}
child_component.html:
<!DOCTYPE html>
<html>
<body>
<polymer-element name="child-component" extends="div">
<template></template>
<script type="application/dart" src="child_component.dart"></script>
</polymer-element>
</body>
</html>
child_component.dart:
import "package:polymer/polymer.dart";
@CustomTag("child-component")
class ChildComponent extends PolymerElement with ObservableMixin {
}
我做错什么了吗?问题出在哪里?
感谢您的回复!
Gabor
我也遇到了类似的问题,错误出现在以下代码中:
DivElement templ = query("#myElem");
var ct = templ.xtag;
//CustomTable ct = templ.xtag;
ct.people = [new Person('aaa', 'aaa'), new Person('vvv', 'vvv')];
其中CustomTable扩展了PolymerElement
幸运的是,它似乎在最新的SDK和Polymer中得到了修复(我尝试了SDK 0.621.3_r26639和Polymer 0.621+3)我检查了你的例子,它也很有效。