Polymer.dart:对子组件使用强类型抛出异常,var起作用

时间:2013-08-06 14:53:01

标签: dart polymer

我有两个用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(包:observe / src / microtask.dart:36:17)

     

#7 wrapMicrotask ..(包:observe / src / microtask.dart:58:35)

     

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

     

#9 runZonedExperimental。 (镖:异步/ 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。 (包:观察/ SRC / microtask.dart:54:25)

     

#14 initPolymer(包装:polymer / polymer.dart: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

1 个答案:

答案 0 :(得分: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和聚合物0.6.21 + 3) 我检查了你的例子,它也可以。

相关问题