报错:Avoid adding reactive properties to a Vue instance or its root $data at runtime - declare it upfr

报错内容:

Avoid adding reactive properties to a Vue instance or its root $data at runtime - declare it upfront in the data option.
在这里插入图片描述

报错代码

在这里插入图片描述

为啥会报错?

知识点
受 ES5 的限制,Vue.js 不能检测到对象属性的添加或删除。因为 Vue.js 在初始化实例时将属性转为 getter/setter,所以属性必须在 data 对象上才能让 Vue.js 转换它,才能让它是响应的。

此时需要借助:this.$set() 方法,
语法:

this.$set(this.data,”key”,value')

Vue.set( target, propertyName/index, value )

此方法接收3个参数,
参数:

  • {Object | Array} target
  • {string | number} propertyName/index
  • {any} value
发布了147 篇原创文章 · 获赞 49 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/bigbear00007/article/details/105254474