2

Per this question here, Vuejs: Computed property not updating the view, I am watching for changes to instances of moment.js, and the view does not update.

Here's an excerpt from the original code that does not work:

set(value) { this.paidFinish = this.paidFinish.hour(value); } 

Here's what I did to fix it:

set(value) { this.paidFinish = this.paidFinish.clone().hour(value); } 

Why does Vuejs not detect the changes? The object is being accessed via this, and the property paidFinish is present in the initial data. (Initially it is set to '', but when the component is mounted it is set to a moment.)

1

1 Answer 1

3

Just had the same problem.

Turns out that the moment object mutates and always returns the same object, so Vue cannot add reactivity to it (see the docs for some clarification about this).

Workaround is either to use frozen-moment library or creating an interval in your component:

data () { return { time: '' } }, beforeMount () { setInterval(() => { this.time = moment().format('HH:mm:ss') }, 1000) } 

Or...

Use date-fns that is immutable

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.