D
D
danilr2019-10-31 09:33:46
Vue.js
danilr, 2019-10-31 09:33:46

How to catch change (get old props and new one) in Vue?

The task is as follows - you need to execute the 'handleChangeTitle' method when changing a certain props ('title').
How to do it? In updated(), I can’t execute it, because it will trigger other props on any state change.
I would like an option similar to the one in React -

componentWillReceiveProps(nextProps) {
//тут сравниваем nextProps.title и this.props.title
 if(nextProps.title !== this.props.title) {
  this.handleChangeTitle()
}

How to get new and old prop at the same time for comparison? What is the equivalent of this in Vue?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ildus Siraev, 2019-10-31
@danilr

Try like this:

watch: {
  title(newVal, oldVal) {
      if(newVal !== oldVal) {
           this.handleChangeTitle()
      }
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question