ZSLN
ZSLN
  • 发布:2024-03-04 17:25
  • 更新:2024-03-04 17:35
  • 阅读:123

watch 监听不到数组的push

分类:uni-app x

<template>
<view>
<view style="height: 100px;width: 100px;background-color: yellow;" @click="clickTop">

    </view>  
    <input type="text" placeholder="文本输入键盘" v-model="buttontext" />  
</view>  

</template>

<script>
export default {
data() {
return {
itemList: [1, 2, 3, 4] as Array<number>,
buttontext: 'xxx'
}
},
watch: {
buttontext(newValue, oldValue) {
console.log("buttontext:", newValue);
},
itemList(newValue, oldValue) {
console.log("itemList:", newValue);
}

    },  
    methods: {  
        clickTop() {  
            console.log('clickTop');  
            // this.itemList.push(3)//通过此方法修改无法监听到  
            // this.itemList[3] = 10;//通过此方法修改无法监听到  
            this.itemList = [2] //这样就可以监听到改变  
            this.buttontext = 'sada'  
        },  
    }  
}  

</script>

<style>

</style>

2024-03-04 17:25 负责人:无 分享
已邀请:
爱豆豆

爱豆豆 - 办法总比困难多

加上deep即可 需要深度监听

watch: {  
            itemList: {  
                handler(newValue, oldValue) {  
                    console.log("itemList:", newValue);  
                },  
                deep: true  
            }  
        },

要回复问题请先登录注册