美文网首页
解决vue侦听器watch,调用this时出现undefined

解决vue侦听器watch,调用this时出现undefined

作者: 扶得一人醉如苏沐晨 | 来源:发表于2022-11-19 18:05 被阅读0次
       watch: {
            'detailData.swcarno': (val) => {
                const upperCase = val.toUpperCase()
                this.detailData.swcarno = upperCase
            }
        },

这里控制台报错

TypeError: Cannot read properties of undefined (reading 'detailData')

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

      watch: {
            'detailData.swcarno': function (val) {
                const upperCase = val.toUpperCase()
                this.detailData.swcarno = upperCase
            }
        },

相关文章

网友评论

      本文标题:解决vue侦听器watch,调用this时出现undefined

      本文链接:https://www.haomeiwen.com/subject/vrtfxdtx.html