Vue中侦听器watch时,调用this时出现undefined问题

简介: Vue中侦听器watch时,调用this时出现undefined问题

watch侦听器中,我们要将新的值赋给this.a出错

watch: {
  value: (newV, oldV) => {
  this.a = newV;
  }
}

这里报错undefined,这里错误的原因是不能写成箭头函数。写成箭头函数后,this会取上下文,而不是组件里面的this了,正确写法为:

watch: {
  value: function(newV, oldV) {
  this.a = newV;
  }
}

如下图:

目录
相关文章
|
6月前
|
JavaScript API
【Vue】Cannot set reactive property on undefined,null,or primitive value:undefined
【Vue】Cannot set reactive property on undefined,null,or primitive value:undefined
95 0
|
3月前
|
JavaScript 前端开发
Vue报错“ Uncaught TypeError: Cannot read property ‘use‘ of undefined”
Vue报错“ Uncaught TypeError: Cannot read property ‘use‘ of undefined”
56 1
|
5月前
|
JavaScript
vue报错: ERROR TypeError: Cannot read property 'version' of undefined
vue报错: ERROR TypeError: Cannot read property 'version' of undefined
29 1
|
7月前
|
JavaScript 前端开发
解决 vue 路由报错 Cannot read property 'beforeRouteEnter' of undefined
解决 vue 路由报错 Cannot read property 'beforeRouteEnter' of undefined
|
10月前
|
人工智能 JavaScript
vue父组件调用子组件this.$refs报错,undefined、not a function问题解决方法
vue父组件调用子组件this.$refs报错,undefined、not a function问题解决方法
|
JavaScript
vue ref获取实例子undefined
当你再created中使用 this.$refs时 dom没有加载成功 所以为undefined 所以你要是想获取 1、要么写在mounted中 dom已经加载完成 2、使用this.$nextTick(function(){})
132 0
vue ref获取实例子undefined
|
JavaScript
Vue整合Echarts报错:“TypeError: Cannot read properties of undefined (reading ‘init‘)“
Vue整合Echarts报错:“TypeError: Cannot read properties of undefined (reading ‘init‘)“
1804 0
Vue整合Echarts报错:“TypeError: Cannot read properties of undefined (reading ‘init‘)“
|
JavaScript
this.$refs 为undefined如何解决?
this.$refs 为undefined如何解决?
this.$refs 为undefined如何解决?
|
JavaScript
vue中使用过滤器this为undefined解决方案
vue中使用过滤器this为undefined解决方案
vue中使用过滤器this为undefined解决方案
|
1月前
|
JavaScript 前端开发 算法
undefined与null的区别
在JavaScript中,undefined和null都表示变量未被赋值或值缺失,但它们在使用场景上有一些区别。 - **`语义不同`**:undefined表示一个变量未被赋值或者声明后未进行初始化。而null表示一个变量被明确地设置为无值或者表示空值的概念。 - **`类型不同`**:undefined是一种基本数据类型,而null是一个引用类型。 - **`条件判断`**:在条件判断中,使用if (variable === undefined)或者if (variable === null)可以进行区分。