vue中 this.$set的用法
生活随笔
收集整理的這篇文章主要介紹了
vue中 this.$set的用法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
當vue的data里邊聲明或者已經賦值過的對象或者數組(數組里邊的值是對象)時,向對象中添加新的屬性,如果更新此屬性的值,是不會更新視圖的。<template><div id="app2"><p v-for="item in items" :key="item.id">{{item.message}}</p><button class="btn" @click="handClick()">更改數據</button> </div>
</template><script>
export default {data() {return {items: [{ message: "one", id: "1" },{ message: "two", id: "2" },{ message: "three", id: "3" }]};},mounted(){this.items[0]={message:"測試",id:"4"}; //此時對象的值更改了,但是視圖沒有更新this.$set(this.items,0,{message:"測試",id:"4"}); //$set可以觸發更新視圖console.log(this.items)},methods: {// 調用方法:Vue.set( target, key, value )// target:要更改的數據源(可以是對象或者數組)// key:要更改的具體數據// value :重新賦的值
handClick() {//Vue methods中的this 指向的是Vue的實例,這里可以直接在this中找到itemsthis.$set(this.items, 0, { message: "更改one的值", id: "0" });},}
};
</script><style>
</style>
?
轉載于:https://www.cnblogs.com/huanhuan55/p/10634793.html
總結
以上是生活随笔為你收集整理的vue中 this.$set的用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 您为了什么而学?【一入红尘深似海 勿负天
- 下一篇: git pull报“unable to