本文记录一下项目中的watch的用法
watch监听全局的projectCode
mounted() {
this.$nextTick(() => {
//初始化读取vuex中的全局参数
if (this.$store.state.user.projectCode) {
this.projectCode = this.$store.state.user.projectCode;
}
});
},
computed:{//配合watch监听使用
selectProject() {
return this.$store.state.user.projectCode;
}
},
watch:{
selectProject: 'selectProject_',
deep: true, //深度监听
immediate: true//以当前的初始值执行handler的函数
},
methods: {
//监听选择项目的值
selectProject_(newVal, oldVal) {
if (newVal) {
this.projectCode = newVal;
}
},
}