欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Vue改變數(shù)組中對(duì)象的屬性不重新渲染View的解決方案

 更新時(shí)間:2018年09月21日 09:54:14   作者:龍恩0707  
這篇文章主要介紹了詳解Vue改變數(shù)組中對(duì)象的屬性不重新渲染View的解決方案,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

在解決問(wèn)題之前,我們先來(lái)了解下 vue響應(yīng)性原理: Vue最顯著的一個(gè)功能是響應(yīng)系統(tǒng)-- 模型只是一個(gè)普通對(duì)象,修改對(duì)象則會(huì)更新視圖。

受到j(luò)avascript的限制,Vue不能檢測(cè)到對(duì)象屬性的添加或刪除,因?yàn)関ue在初始化實(shí)列時(shí)將屬性轉(zhuǎn)為getter/setter,所以屬性必須在data對(duì)象上才能讓vue轉(zhuǎn)換它。

但是vue可以使用 Vue.set(object, key, value)方法將響應(yīng)屬性添加到嵌套的對(duì)象上:如下代碼:

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

或者可以使用vm.$set的實(shí)列方法,也是Vue.set方法的別名:

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

問(wèn)題: 頁(yè)面上多個(gè)item項(xiàng), 當(dāng)我鼠標(biāo)移動(dòng)上去的時(shí)候,我想在該數(shù)組中的對(duì)象添加一個(gè)屬性 isHover=true, 當(dāng)鼠標(biāo)移出的時(shí)候,我想讓該屬性變?yōu)?isHover=false,然后希望改變對(duì)象的屬性的時(shí)候讓其重新渲染view層,重新執(zhí)行rowClasses方法,然后該方法會(huì)判斷 isHover是否等于true,如果為true的話,讓其增加一個(gè)類名。

代碼如下:

<!DOCTYPE html>
<html>
 <head>
  <title>演示Vue</title>
  <style>
   * {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;}
  </style>
 </head>
 <body>
  <div id='app'>
   <ul>
    <li 
     v-for="(item, index) in items" 
     @mouseenter.stop="handleMouseIn(index)"
     @mouseleave.stop="handleMouseOut(index)"
     :class="rowClasses(index)"
    >
     <span>{{item.name}}</span>
    </li>
   </ul>
  </div>
 </body>
 <script src="https://tugenhua0707.github.io/vue/vue1/vue.js"></script>
 <script type="text/javascript">
  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); // 可以執(zhí)行到
     this.$data.items[index]._isHover = true;
    },
    handleMouseOut(index) {
     this.$data.items[index]._isHover = false;
    }
   }
  });
 </script>
</html>

查看效果

可以看到鼠標(biāo)移動(dòng)上去的時(shí)候 沒(méi)有效果。

解決的方案如下:

1. 使用 Object.assign

鼠標(biāo)移動(dòng)上去的時(shí)候 代碼可以改成如下:

this.$data.items[index]._isHover = true;
this.$data.items = Object.assign({}, this.$data.items);

鼠標(biāo)移出的時(shí)候,代碼改成如下:

this.$data.items[index]._isHover = false;
this.$data.items = Object.assign({}, this.$data.items);

代碼如下:

<!DOCTYPE html>
<html>
 <head>
  <title>演示Vue</title>
  <style>
   * {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;}
  </style>
 </head>
 <body>
  <div id='app'>
   <ul>
    <li 
     v-for="(item, index) in items" 
     @mouseenter.stop="handleMouseIn(index)"
     @mouseleave.stop="handleMouseOut(index)"
     :class="rowClasses(index)"
    >
     <span>{{item.name}}</span>
    </li>
   </ul>
  </div>
 </body>
 <script src="https://tugenhua0707.github.io/vue/vue1/vue.js"></script>
 <script type="text/javascript">
  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); // 可以執(zhí)行到
     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);
    }
   }
  });
 </script>
</html>

查看效果

2. 使用Vue.set(object, key, value)方法將響應(yīng)屬性添加到嵌套的對(duì)象上。

鼠標(biāo)移動(dòng)上去的時(shí)候 代碼可以改成如下:

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

鼠標(biāo)移出的時(shí)候,代碼改成如下:

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

所有的代碼如下:

<!DOCTYPE html>
<html>
 <head>
  <title>演示Vue</title>
  <style>
   * {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;}
  </style>
 </head>
 <body>
  <div id='app'>
   <ul>
    <li 
     v-for="(item, index) in items" 
     @mouseenter.stop="handleMouseIn(index)"
     @mouseleave.stop="handleMouseOut(index)"
     :class="rowClasses(index)"
    >
     <span>{{item.name}}</span>
    </li>
   </ul>
  </div>
 </body>
 <script src="https://tugenhua0707.github.io/vue/vue1/vue.js"></script>
 <script type="text/javascript">
  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); // 可以執(zhí)行到
     this.$set(this.$data.items[index], '_isHover', true);
    },
    handleMouseOut(index) {
     this.$set(this.$data.items[index], '_isHover', false);
    }
   }
  });
 </script>
</html>

查看效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue + Vue-router 同名路由切換數(shù)據(jù)不更新的方法

    Vue + Vue-router 同名路由切換數(shù)據(jù)不更新的方法

    本篇文章主要介紹了Vue + Vue-router 同名路由切換數(shù)據(jù)不更新的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Vue異步組件處理路由組件加載狀態(tài)的解決方案

    Vue異步組件處理路由組件加載狀態(tài)的解決方案

    這篇文章主要介紹了Vue異步組件處理路由組件加載狀態(tài)的解決方案,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-09-09
  • 關(guān)于vue二進(jìn)制轉(zhuǎn)圖片顯示問(wèn)題 后端返回的是byte[]數(shù)組

    關(guān)于vue二進(jìn)制轉(zhuǎn)圖片顯示問(wèn)題 后端返回的是byte[]數(shù)組

    這篇文章主要介紹了關(guān)于vue二進(jìn)制轉(zhuǎn)圖片顯示問(wèn)題 后端返回的是byte[]數(shù)組,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 解決Vue + Echarts 使用markLine標(biāo)線(precision精度問(wèn)題)

    解決Vue + Echarts 使用markLine標(biāo)線(precision精度問(wèn)題)

    這篇文章主要介紹了解決Vue + Echarts 使用markLine標(biāo)線(precision精度問(wèn)題),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • vue-vuex中使用commit提交mutation來(lái)修改state的方法詳解

    vue-vuex中使用commit提交mutation來(lái)修改state的方法詳解

    今天小編就為大家分享一篇vue-vuex中使用commit提交mutation來(lái)修改state的方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • 在Vue開發(fā)過(guò)程中解決和預(yù)防內(nèi)存泄漏問(wèn)題的方法詳解

    在Vue開發(fā)過(guò)程中解決和預(yù)防內(nèi)存泄漏問(wèn)題的方法詳解

    Vue作為一款流行的前端框架,已經(jīng)在許多項(xiàng)目中得到廣泛應(yīng)用,然而,隨著我們?cè)赩ue中構(gòu)建更大規(guī)模的應(yīng)用程序,我們可能會(huì)遇到一個(gè)嚴(yán)重的問(wèn)題,那就是內(nèi)存泄漏,因此,我們需要認(rèn)識(shí)到在Vue開發(fā)過(guò)程中,內(nèi)存泄漏問(wèn)題的重要性,本文將給大家介紹如何解決和預(yù)防內(nèi)存泄漏問(wèn)題
    2023-10-10
  • Vue波紋按鈕組件制作

    Vue波紋按鈕組件制作

    本文給大家分享了VUE制作點(diǎn)擊按鈕出現(xiàn)水波紋效果的組件過(guò)程,對(duì)此有需求的朋友可以跟著學(xué)習(xí)下。
    2018-04-04
  • 關(guān)于vue3編寫掛載DOM的插件問(wèn)題

    關(guān)于vue3編寫掛載DOM的插件問(wèn)題

    這篇文章主要介紹了vue3編寫掛載DOM的插件的問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-07-07
  • 深入理解vue中的slot與slot-scope

    深入理解vue中的slot與slot-scope

    這篇文章主要介紹了vue中的slot與slot-scope的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-04-04
  • vue自定義指令directive的使用方法

    vue自定義指令directive的使用方法

    這篇文章主要介紹了vue自定義指令directive的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04

最新評(píng)論