我在使用Polymer.js的Firefox上遇到了问题。
这是我的Polymer元素。
<!-- Imports polymer -->
<link rel="import" href="../../bower_components/polymer/polymer.html">
<!-- Defines element markup -->
<dom-module id="scoreboard-bar">
...
<script>
Polymer({
is: 'scoreboard-bar',
properties: {
totalCounter: {
type: Number,
value: 0
},
},
getTotalCounter: function () {
return this.totalCounter;
},
setTotalCounter: function (totalCounter) {
this.totalCounter = totalCounter;
},
});
</script>
</dom-module>
这是将Polymer组件导入主html文件:
<head>
<!-- Imports polyfill -->
<script src="{% static "polymer/bower_components/webcomponentsjs/webcomponents-lite.min.js" %}"> </script>
<!-- Imports custom elements -->
<link rel="import" href="{% static "polymer/components/entity-vibeboard/scoreboard-bar.html" %}">
</head>
要在加载我拥有的页面时设置计数器:
<body>
<div>
<scoreboard-bar id="scoreboardBarFlag"></scoreboard-bar>
</div>
<script>
$( document ).ready(function() {
var scoreboardBarFlagPolymer = document.querySelector('#scoreboardBarFlag');
scoreboardBarFlagPolymer.setTotalCounter(10);
});
</script>
</body>
此代码在Google Chrome上完美运行,但它不适用于Firefox。我收到的错误是:
TypeError: scoreboardBarFlagPolymer.setTotalCounter is not a function
如果我在Firebug上添加一个断点,进行调试,该功能在Polymer组件上可用,但在加载页面后它不可用。
我认为错误是因为javascript函数试图在页面上加载Polymer组件之前设置计数器。
任何想法如何在加载页面上的所有Polymer组件后运行javascript函数?问题只出在Firefox上,在Chrome上运行正常。
答案 0 :(得分:2)
尝试按in the documentation of webcomponents.js
所述聆听WebComponentsReady
事件
window.addEventListener('WebComponentsReady', function(e) {
// imports are loaded and elements have been registered
console.log('Components are ready');
});