我有两个用Polymer.dart编写的自定义组件,一个是另一个的父组件。我想获得对父级中子级组件的引用,但是如果使用类型注释,则会出现异常。另一方面,如果我使用var
关键字声明变量,则可以正常工作。
使用类型时出现的错误消息和堆栈跟踪:
Uncaught Error: type 'ChildComponent' is not a subtype of type 'ChildComponent' of 'childComponent'.
Stack Trace:
#0 ParentComponent.inserted (package:types_test_lib/parent_component.dart?1375856554489:9:67)
#1 _registerLifecycleInsert. (package: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 performMicrotaskCheckpoint (package:observe/src/microtask.dart:36:17)
#7 wrapMicrotask.. (package:observe/src/microtask.dart:58:35)
#8 runZonedExperimental (dart:async/zone.dart:259:53)
#9 runZonedExperimental. (dart:async/zone.dart: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 wrapMicrotask. (package:observe/src/microtask.dart:54:25)
#14 initPolymer (package:polymer/polymer.dart:72:5)
#15 main (.../D:/workspace/dart/types_test/web/types_test.html:7:22)
Exception: type 'ChildComponent' is not a subtype of type 'ChildComponent' of '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 {
}
难道我做错了什么?可能是什么问题?感谢您的任何回复!
加博尔
最佳答案
我有类似的问题,该错误出现在以下代码中:
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.6.21.3_r26639和Polymer 0.6.21 + 3)
我检查了您的示例,它也很好。
关于dart - Polymer.dart:对子组件使用强类型会引发异常,var工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18083576/