原因
为什么要输出这个,this.$set
不是用来设置的函数吗?
解决方法
是不是用错函数了?
Vue.set()和this.$set()这两个api的实现原理基本一模一样,都是使用了set函数。
set函数是从 ../observer/index 文件中导出的,区别在于Vue.set()是将set函数绑定在Vue构造函数上,this.$set()是将set函数绑定在Vue原型上。
参考用法:
mounted () {
this.$set(this.student,"age", 24)
}
如有问题及时沟通
data有定义menu_btn吗
感觉this指向不对,你console.log('this', this) 看看是不是指向了window对象
自己瞎搞,不定义就乱打印
vue3.0 去除了 $set这个api
参考:https://v3.cn.vuejs.org/api/instance-methods.html#watch
Vue 报错TypeError: this.$set is not a function 的解决方法
报错场景:将APi中得到的response数据,用Vue$set()使数据动态响应
报错代码:
methods: {
textTranslate: function (text, to) {
$.ajax({
url: 'http://openapi.youdao.com/api',
type: 'post',
dataType: 'jsonp',
data: {
q: text,
appKey: this.appKey,
salt: this.salt,
from: this.from,
to: to,
sign: md5(this.appKey + text + this.salt + this.key)
},
success: function (data) {
this.$set(this.$data, 'translatedText', data.translation[0])
}
})
}
}
报错原因:这里的this指向的不是VueModel,
解决方法1:在执行函数中定义指向Model的变量 let vm = this ,用该变量替代this
methods: {
textTranslate: function (text, to) {
let vm = this
$.ajax({
url: 'http://openapi.youdao.com/api',
type: 'post',
dataType: 'jsonp',
data: {
q: text,
appKey: this.appKey,
salt: this.salt,
from: this.from,
to: to,
sign: md5(this.appKey + text + this.salt + this.key)
},
success: function (data) {
vm.$set(vm.$data, 'translatedText', data.translation[0])
}
})
}
}
解决方法2:将。siccess改为箭头函数的写法,这样子箭头函数里的this其实是指向VueModel的,这样子用this看不会报错了
success: (data) => {
this.$set(this.$data, 'translatedText', data.translation[0])
}