Vue 列表渲染指令

改變數(shù)據(jù)同時(shí)也修改頁(yè)面的方法

方法一:通過數(shù)組變異的方法:push pop shift unshift splice sort reverse
方法二:通過改變對(duì)象引用
方法三:通過set方法

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title></title>
  <script src='./vue.js'></script>
</head>

<body>
  <div id="app">
    <div v-for="(item,index) of list" :key="item.id">
      <div>{{item.text}} --- {{index}}</div>
      <span>{{item.text}}</span>
    </div>
  </div>

  <script>
    // Vue中不能通過下標(biāo)來改變頁(yè)面數(shù)組中的數(shù)據(jù),例如 vm.list[1]={id:333,text:"ddg"}
    // 只能通過下列方法:push pop shift unshift splice sort reverse
    // 或者通過改變對(duì)象的引用地址:vm.list=[{id: 010011001,text: "hello"}, {id: 010011002,text: "puxiaotaoc"}, {id: 010011003,text: "nice"}]
    var vm = new Vue({
      el: '#app',
      data: {
        list: [{
          id: 010011001,
          text: "hello"
        }, {
          id: 010011002,
          text: "puxiaotao"
        }, {
          id: 010011003,
          text: "nice"
        }]
      }
    })
  </script>
</body>

</html>
// 對(duì)象的操作
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title></title>
  <script src='./vue.js'></script>
</head>

<body>
  <div id="app">
    <div v-for="(item,key,index) of userInfo">{{item}} --- {{key}} --- {{index}}</div>
  </div>

  <script>
    var vm = new Vue({
      // 實(shí)現(xiàn)改變數(shù)據(jù)也改變頁(yè)面
      // userInfo: {
      //   name: "puxiaotaoc",
      //   age: 28,
      //   gender: "female",
      //   address: "wuhan"
      // } 改變對(duì)象的引用
      // Vue.set(vm.userInfo,"address","wuhan") 全局方法
      // vm.$set(vm.userInfo,"address","wuhan") 實(shí)例方法
      el: '#app',
      data: {
        userInfo: {
          name: "puxiaotaoc",
          age: 28,
          gender: "female"
        }
      }
    })
  </script>
</body>

</html>
// 數(shù)組的操作
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title></title>
  <script src='./vue.js'></script>
</head>

<body>
  <div id="app">
    <div v-for="(item,index) of userInfo">{{item}} --- {{index}}</div>
  </div>

  <script>
    // Vue.set(vm.userInfo,1,5) 全局方法
    // vm.$set(vm.userInfo,2,10) 實(shí)例方法
    var vm = new Vue({
      el: '#app',
      data: {
        userInfo: [1, 2, 3]
      }
    })
  </script>
</body>

</html>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容