聚合查询选择器在DartVM上工作但在编译后不在Chrome中

Fai*_*bid 4 dart dart-polymer

有一个奇怪的问题.在我的Dart代码中,我在屏幕上有一些聚合物组件,其中一个有我从main()调用的方法.

我抓住它的参考

PolyComp poly = querySelector("#idOfPolymer");
poly.flash();
Run Code Online (Sandbox Code Playgroud)

这在飞镖中完美无缺.页面加载并且PolyComp开始闪烁.但是当我通过从Dart IDE运行Build Polymer应用程序在Chrome中运行它时,我收到一条错误,指出无法在null上调用flash().

我最后只是使用一个事件总线让PolyComp听我的事件,但是这太过分了.

我究竟做错了什么?这种情况发生在最新的Chrome,Firefox和Safari中.

编辑:

我还为JS构建了以下聚合物应用程序并遇到了同样的问题. https://github.com/sethladd/dart-polymer-dart-examples/blob/master/web/todo_element/todo.html

适用于DartVM,而不是Chrome,因为它在null元素上调用方法.

Gün*_*uer 7

当您从该main()方法运行此代码时,它可能是一个计时问题.你可以尝试类似的东西

import "package:polymer/polymer.dart";

main() {
  initPolymer().run(() {
    // code here works most of the time
    Polymer.onReady.then((e) {     
      // some things must wait until onReady callback is called
    });
  });
}
Run Code Online (Sandbox Code Playgroud)

另请参阅如何在聚合物应用程序中实现主要功能