聚合查询选择器在 DartVM 上运行,但在编译后不在 Chrome 中运行 [英] Polymer querySelector working on DartVM but not in Chrome after compile
问题描述
遇到一个奇怪的问题.在我的 Dart 代码中,我在屏幕上有一些聚合物组件,其中一个有我从 main() 调用的方法.
Having a weird issue. In my Dart code I have some polymer components on the screen and one of them has a method I call from my main().
我通过执行获取对它的引用
I grab a reference to it by doing
PolyComp poly = querySelector("#idOfPolymer");
poly.flash();
这在 dart 中非常有效.页面加载完毕,PolyComp 开始闪烁.但是,当我通过从 Dart IDE 运行 Build Polymer 应用程序在 Chrome 中运行它时,我收到一条错误消息,提示无法在 null 上调用 flash().
This works perfectly in dart. The page loads up and PolyComp starts to flash. However when I run this in Chrome by running Build Polymer app from the Dart IDE, I get an error that says cannot call flash() on null.
我最终仅通过使用事件总线并让 PolyComp 监听我的事件来使其闪烁,但这太过分了.
I ended up making it flash by just using an event bus and letting PolyComp listen to my event, but this is overkill.
我做错了什么?这种情况发生在最新的 Chrome、Firefox 和 Safari 中.
What am I doing wrong? This happens in the latest Chrome, Firefox and Safari.
我也为 JS 构建了以下聚合物应用程序,但遇到了同样的问题.https://github.com/sethladd/dart-polymer-dart-examples/blob/master/web/todo_element/todo.html
I built the following polymer app to JS also and ran into the same issue. https://github.com/sethladd/dart-polymer-dart-examples/blob/master/web/todo_element/todo.html
适用于 DartVM,不适用于 Chrome,因为它在空元素上调用方法.
Works on DartVM, not in Chrome because its calling a method on a null element.
推荐答案
当您从 main()
方法运行此代码时,可能是时间问题.您可以尝试类似
When you run this code from the main()
method it is probably a timing issue.
You can try something like
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
});
});
}
这篇关于聚合查询选择器在 DartVM 上运行,但在编译后不在 Chrome 中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!