如何在 Polymer 2 的 moment-js 组件中每分钟更新一次时间?
How to update time every minute in moment-js component in Polymer 2?
我在 Polymer 2 中使用 moment-js 组件。时间不是每分钟更新一次,我必须刷新页面才能更新时间。如何在不刷新页面的情况下每分钟更新一次时间?
我正在使用以下代码:
<moment-js class="clock title" format="hh:mm A"></moment-js>
您可以使用受保护的_updateFormattedDateMoment()
函数。
在你的父组件中你可以这样做:
created: function() {
this.timer = setInterval(this.updateDate, 1000);
}
updateDate: function() {
var moment = this.root.querySelector('.clock');
moment._updateFormattedDateMoment();
}
正如 Umit 指出的那样,moment-js
中的 _updateFormattedDateMoment()
函数会更新显示的时间。这就是您在 Polymer 2 中使用它的方式:
class XFoo extends Polymer.Element {
static get is() { return 'x-foo'; }
connectedCallback() {
super.connectedCallback();
setInterval(() => this.updateDate(), 1000);
}
updateDate() {
// assumes <moment-js id="clock" ...>
this.$.clock._updateFormattedDateMoment();
}
}
customElements.define(XFoo.is, XFoo);
我在 Polymer 2 中使用 moment-js 组件。时间不是每分钟更新一次,我必须刷新页面才能更新时间。如何在不刷新页面的情况下每分钟更新一次时间?
我正在使用以下代码:
<moment-js class="clock title" format="hh:mm A"></moment-js>
您可以使用受保护的_updateFormattedDateMoment()
函数。
在你的父组件中你可以这样做:
created: function() {
this.timer = setInterval(this.updateDate, 1000);
}
updateDate: function() {
var moment = this.root.querySelector('.clock');
moment._updateFormattedDateMoment();
}
正如 Umit 指出的那样,moment-js
中的 _updateFormattedDateMoment()
函数会更新显示的时间。这就是您在 Polymer 2 中使用它的方式:
class XFoo extends Polymer.Element {
static get is() { return 'x-foo'; }
connectedCallback() {
super.connectedCallback();
setInterval(() => this.updateDate(), 1000);
}
updateDate() {
// assumes <moment-js id="clock" ...>
this.$.clock._updateFormattedDateMoment();
}
}
customElements.define(XFoo.is, XFoo);