<template>
<div>
lazy_tomato
<h2>{{ obj }}</h2>
<button @click="handleChange">点击我给obj赋值</button>
</div>
</template>
<script>
export default {
data() {
return {
obj: {},
}
},
methods: {
handleChange() {
// 错误代码二 typeError: Cannot read properties of undefined (reading '__ob__')
// this.$set(this.obj.a, 'b.c', '爱吃番茄')
// 正确代码
this.$set(this.obj, 'a', { b: { c: '爱吃番茄' } })
console.log(JSON.stringify(this.obj))
},
},
}
</script>