Related uses of watch in vue

  • basic usage

 Use watch to monitor a variable, and when the variable changes, the method in the watch will be executed

The following name is the variable declared in vue data. When the value of name changes, the code inside will be executed.

watch: {
    name(new,old) {
       //你要执行的代码
        ....
       console.log(news,old)
	}
},
  • handler() method

If you want the code to be executed from the beginning, that is, it will be executed immediately when the name value has not changed at the beginning, you can use the handler method to set the value of the property immediate to true, if it is false, the execution effect is the same as the above basic usage

watch: {
    name:{
        handler(new,old) {
           //你要执行的代码
            ....
            console.log(news,old)
	    },
        immediate: true
    }
},

This watch can not only monitor the variables in the data, but also monitor the changes in the route. You only need to change the above variable name to $route, and the usage is the same.

Guess you like

Origin blog.csdn.net/miao_yf/article/details/105268469