如何在 polymer-starter-kit 的顶层 属性 上获得通知事件?
How do I get a notify event on a property that's at the top level in the polymer-starter-kit?
所以基本入门工具包有一个变量 'route'(显示的当前页面的字符串),如果它是一个组件,我只需将 notify:true 添加到属性中JSON,它是 Polymer(...) 调用的一部分。
但该示例并未将顶层包装在 Polymer(...) 中,那么如何将其设置为在更改时通知?
我可以将事件侦听器添加到 #app 中,但它不会在不知道必须发送该事件的情况下发送该事件。
我想要做的就是在当前页面更改时获得一个事件,所以我确信我只是忽略了一些非常明显的东西。
您可以简单地创建一个绑定到顶级属性并触发事件的元素。
所以在你的 index.html:
<body ...>
<template is="dom-bind">
<my-route-watcher route="{{route}}">
</template>
</body>
在你的路线中-watcher.html:
<dom-module id="my-route-watcher">
<script>
Polymer({
is: "my-route-watcher",
properties: {
route: {
type: String,
notify: true
}
}
routeChanged: function() {
this.fire('the-route-changed', { msg: "woohoo" });
}
});
</script>
</dom-module>
所以基本入门工具包有一个变量 'route'(显示的当前页面的字符串),如果它是一个组件,我只需将 notify:true 添加到属性中JSON,它是 Polymer(...) 调用的一部分。
但该示例并未将顶层包装在 Polymer(...) 中,那么如何将其设置为在更改时通知?
我可以将事件侦听器添加到 #app 中,但它不会在不知道必须发送该事件的情况下发送该事件。
我想要做的就是在当前页面更改时获得一个事件,所以我确信我只是忽略了一些非常明显的东西。
您可以简单地创建一个绑定到顶级属性并触发事件的元素。
所以在你的 index.html:
<body ...>
<template is="dom-bind">
<my-route-watcher route="{{route}}">
</template>
</body>
在你的路线中-watcher.html:
<dom-module id="my-route-watcher">
<script>
Polymer({
is: "my-route-watcher",
properties: {
route: {
type: String,
notify: true
}
}
routeChanged: function() {
this.fire('the-route-changed', { msg: "woohoo" });
}
});
</script>
</dom-module>