Uncaught TypeError: Cannot read property of undefined

简介: vue中出现这种错误导致这种错误产生的原因是在选项 property 或回调上使用箭头函数created: () => console.log(this.a) 或 vm.$watch('a', newValue => this.myMethod())12因为箭头函数并没有 this,this 会作为变量一直向上级词法作用域查找,直至找到为止

vue中出现这种错误

Uncaught TypeError: Cannot read property of undefined
 或 Uncaught TypeError: this.myMethod is not a function


导致这种错误产生的原因是在选项 property 或回调上使用箭头函数

created: () => console.log(this.a) 
或 vm.$watch('a', newValue => this.myMethod())

因为箭头函数并没有 this,this 会作为变量一直向上级词法作用域查找,直至找到为止

目录
相关文章
|
2天前
|
JSON 数据格式
setInterval函数的function与()=>区别——解决Cannot readproperty‘XXXXXXX‘of undefined异常
setInterval函数的function与()=>区别——解决Cannot readproperty‘XXXXXXX‘of undefined异常
3 0
|
1月前
webpack版本问题 Cannot read property ‘createHash‘ of undefined
webpack版本问题 Cannot read property ‘createHash‘ of undefined
|
1月前
|
iOS开发 MacOS
TypeError: Cannot read property ‘shop‘ of undefined
TypeError: Cannot read property ‘shop‘ of undefined
17 0
|
1月前
|
JavaScript
JS中Null和Undefined的区别及用法
JS中Null和Undefined的区别及用法
19 1
|
1月前
|
JavaScript 前端开发 算法
undefined与null的区别
在JavaScript中,undefined和null都表示变量未被赋值或值缺失,但它们在使用场景上有一些区别。 - **`语义不同`**:undefined表示一个变量未被赋值或者声明后未进行初始化。而null表示一个变量被明确地设置为无值或者表示空值的概念。 - **`类型不同`**:undefined是一种基本数据类型,而null是一个引用类型。 - **`条件判断`**:在条件判断中,使用if (variable === undefined)或者if (variable === null)可以进行区分。
|
1月前
|
JavaScript 前端开发 程序员
分享18个用于处理 null、NaN 和undefined 的 JS 代码片段
Null、NaN 和 undefined 是程序员在使用 JavaScript 时遇到的常见值。 有效处理这些值对于确保代码的稳定性和可靠性至关重要。
|
1月前
|
JavaScript 前端开发 API
null和undefined:两个JavaScript中的特殊值(二)
null和undefined:两个JavaScript中的特殊值
|
1月前
|
JavaScript 前端开发 安全
null和undefined:两个JavaScript中的特殊值(一)
null和undefined:两个JavaScript中的特殊值
|
9月前
【已解决】TypeError: Cannot destructure property `createHash` of ‘undefined‘ or ‘null‘
【已解决】TypeError: Cannot destructure property `createHash` of ‘undefined‘ or ‘null‘
224 1
|
1月前
|
前端开发 JavaScript
【Web 前端】undefined 和 null 区别?
【4月更文挑战第22天】【Web 前端】undefined 和 null 区别?
【Web 前端】undefined 和 null 区别?