1

我有两个用 Polymer.dart 编写的自定义组件,一个是另一个的父级。我想在父组件中获取对子组件的引用,但是如果我使用类型注释,则会出现异常。另一方面,如果我使用var关键字声明变量,它工作正常。

使用类型时得到的错误消息和堆栈跟踪:

未捕获的错误:“ChildComponent”类型不是“childComponent”的“ChildComponent”类型的子类型。

堆栈跟踪:

#0 ParentComponent.inserted (package:types_test_lib/parent_component.dart?1375856554489:9:67)

#1 _registerLifecycleInsert。(包: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..(包:observe/src/microtask.dart:58:35)

#8 runZonedExperimental (dart:async/zone.dart:259:53)

#9 运行分区实验。(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 包装微任务。(包:observe/src/microtask.dart:54:25)

#14 initPolymer(包装:聚合物/聚合物.dart:72:5)

#15 主要(.../D:/workspace/dart/types_test/web/types_test.html:7:22)

例外:“ChildComponent”类型不是“childComponent”的“ChildComponent”类型的子类型。

索引.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>

父组件.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>

父组件.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 {
  
}

难道我做错了什么?可能是什么问题?

感谢您的任何回复!

嘉宝

4

1 回答 1

0

我有类似的问题,错误出现在以下代码中:

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)我检查了你的示例,它也可以正常工作。

于 2013-08-27T14:09:04.560 回答