vue 对象中数组中对象某个属性更改_详解Vue改变数组中对象的属性不重新渲染View的解决方案...

在解决问题之前,我们先来了解下 vue响应性原理: Vue最显著的一个功能是响应系统-- 模型只是一个普通对象,修改对象则会更新视图。

受到javascript的限制,Vue不能检测到对象属性的添加或删除,因为vue在初始化实列时将属性转为getter/setter,所以属性必须在data对象上才能让vue转换它。

但是vue可以使用 Vue.set(object, key, value)方法将响应属性添加到嵌套的对象上:如下代码:

Vue.set(obj, '_isHover', true);

或者可以使用vm.$set的实列方法,也是Vue.set方法的别名:

this.$set(obj, '_isHover', false);

问题: 页面上多个item项, 当我鼠标移动上去的时候,我想在该数组中的对象添加一个属性 isHover=true, 当鼠标移出的时候,我想让该属性变为 isHover=false,然后希望改变对象的属性的时候让其重新渲染view层,重新执行rowClasses方法,然后该方法会判断 isHover是否等于true,如果为true的话,让其增加一个类名。

代码如下:

演示Vue

* {margin: 0; padding: 0;}

ul, li {list-style: none;}

#app {width: 800px; margin: 20px auto; border:1px solid #ccc; border-bottom: none;}

#app li {height: 32px; line-height: 32px; border-bottom: 1px solid #ccc;}

#app li.bgColor {background-color: red;}

  • v-for="(item, index) in items"

    @mouseenter.stop="handleMouseIn(index)"

    @mouseleave.stop="handleMouseOut(index)"

    :class="rowClasses(index)"

    >

    {{item.name}}

new Vue({

el: '#app',

data: {

items: [

{name: 'kongzhi'},

{name: 'longen'},

{name: 'tugenhua'}

]

},

computed: {

},

methods: {

rowClasses (index) {

return [

{

[`bgColor`]: this.$data.items[index] && this.$data.items[index]._isHover

}

]

},

handleMouseIn(index) {

if (this.$data.items[index]._isHover) {

return;

}

console.log(111); // 可以执行到

this.$data.items[index]._isHover = true;

},

handleMouseOut(index) {

this.$data.items[index]._isHover = false;

}

}

});

可以看到鼠标移动上去的时候 没有效果。

解决的方案如下:

1. 使用 Object.assign

鼠标移动上去的时候 代码可以改成如下:

this.$data.items[index]._isHover = true;

this.$data.items = Object.assign({}, this.$data.items);

鼠标移出的时候,代码改成如下:

this.$data.items[index]._isHover = false;

this.$data.items = Object.assign({}, this.$data.items);

代码如下:

演示Vue

* {margin: 0; padding: 0;}

ul, li {list-style: none;}

#app {width: 800px; margin: 20px auto; border:1px solid #ccc; border-bottom: none;}

#app li {height: 32px; line-height: 32px; border-bottom: 1px solid #ccc;}

#app li.bgColor {background-color: red;}

  • v-for="(item, index) in items"

    @mouseenter.stop="handleMouseIn(index)"

    @mouseleave.stop="handleMouseOut(index)"

    :class="rowClasses(index)"

    >

    {{item.name}}

new Vue({

el: '#app',

data: {

items: [

{name: 'kongzhi'},

{name: 'longen'},

{name: 'tugenhua'}

]

},

computed: {

},

methods: {

rowClasses (index) {

return [

{

[`bgColor`]: this.$data.items[index] && this.$data.items[index]._isHover

}

]

},

handleMouseIn(index) {

if (this.$data.items[index]._isHover) {

return;

}

console.log(111); // 可以执行到

this.$data.items[index]._isHover = true;

this.$data.items = Object.assign({}, this.$data.items);

},

handleMouseOut(index) {

this.$data.items[index]._isHover = false;

this.$data.items = Object.assign({}, this.$data.items);

}

}

});

2. 使用Vue.set(object, key, value)方法将响应属性添加到嵌套的对象上。

鼠标移动上去的时候 代码可以改成如下:

this.$set(this.$data.items[index], '_isHover', true);

鼠标移出的时候,代码改成如下:

this.$set(this.$data.items[index], '_isHover', false);

所有的代码如下:

演示Vue

* {margin: 0; padding: 0;}

ul, li {list-style: none;}

#app {width: 800px; margin: 20px auto; border:1px solid #ccc; border-bottom: none;}

#app li {height: 32px; line-height: 32px; border-bottom: 1px solid #ccc;}

#app li.bgColor {background-color: red;}

  • v-for="(item, index) in items"

    @mouseenter.stop="handleMouseIn(index)"

    @mouseleave.stop="handleMouseOut(index)"

    :class="rowClasses(index)"

    >

    {{item.name}}

new Vue({

el: '#app',

data: {

items: [

{name: 'kongzhi'},

{name: 'longen'},

{name: 'tugenhua'}

]

},

computed: {

},

methods: {

rowClasses (index) {

return [

{

[`bgColor`]: this.$data.items[index] && this.$data.items[index]._isHover

}

]

},

handleMouseIn(index) {

if (this.$data.items[index]._isHover) {

return;

}

console.log(111); // 可以执行到

this.$set(this.$data.items[index], '_isHover', true);

},

handleMouseOut(index) {

this.$set(this.$data.items[index], '_isHover', false);

}

}

});

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值