vue.js實現資料動態響應(Vue.set的應用)

來源:互聯網
上載者:User

標籤:oct   分享   img   images   更新   數組   ges   asc   公司   

在vue裡面,我們操作最多的就是各種資料,在jquery裡面,我們習慣通過下標定向找到資料,然後重新賦值

比如var a[0]=111;(希望上家公司原諒菜鳥的我寫了不少這樣的代碼??)

下面上代碼

<html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title>    <script src="./js/vue.min.js"></script></head><body>    <div id="app">         <ul>             <li v-for="item in listData">{{item}}</li>        </ul>        <a href="javascript:void(0)" v-text="he" @click="changeData()"></a>    </div></body><script>    new Vue({        el:"#app",        data:{            he:"點我",            listData:["a","b","c"]          },        methods:{              changeData () {                  this.listData[0]="d";              }            }    })</script></html>

當我點擊按鈕時候,發現沒有任何變化,頁面上還是a,b,c

vue當然不會這麼菜呢

下面是偉大的vue內建的方法來了

 Vue.set()

官方解釋

設定對象的屬性。如果對象是響應式的,確保屬性被建立後也是響應式的,同時觸發視圖更新。這個方法主要用於避開 Vue 不能檢測屬性被添加的限制。

 

好長喲,哈哈哈哈??

我的理解就是觸發視圖重新更新一遍,資料動態起來

Vue.set(a,b,c)

a是要更改的資料

b是資料的第幾項

c是更改後的資料

解決上面資料不能更改後的代碼
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title>    <script src="./js/vue.min.js"></script></head><body>    <div id="app">         <ul>             <li v-for="item in listData">{{item}}</li>        </ul>        <a href="javascript:void(0)" v-text="he" @click="changeData()"></a>    </div></body><script>    new Vue({        el:"#app",        data:{            he:"點我",            listData:["a","b","c"]          },        methods:{              changeData () {                  Vue.set(this.listData,0,‘X‘)              }            }    })</script></html>

我們可以看到,this.listData數組的第一項已經被更改了

X

 

vue.js實現資料動態響應(Vue.set的應用)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.