uni-app v-for數據的綁定唯一


uni-app v-for數據的綁定唯一

  這個其實Vue里我已經寫了, 比這個全,不過既然講到了就有打了一遍

  中間出了一個錯誤, 搞了老半天, 結果重啟一下就好了, 浪費了好多時間, 坑!

 1 <template>
 2     <view>
 3         <view v-for="stu in studentArry" :key="stu.id">     <!--:key 保證組件和數據捆綁唯一 -->
 4             <label class="checkbox">
 5                 <checkbox :value="stu.name" /> {{ stu.name }}
 6             </label>
 7         </view>
 8         
 9         <button type="primary" @click="addStu">新增學生</button>
10     </view>
11 </template>
12 
13 <script>
14     export default {
15         data() {
16             return {
17                 studentArry: [
18                             {
19                                 id:1,
20                                 name: '小明',
21                                 age: 18,
22                             },
23                             {
24                                 id:2,
25                                 name: '小紅',
26                                 age: 19,
27                             },
28                             {
29                                 id:3,
30                                 name: '小李',
31                                 age: 17,
32                             }
33                         ]
34                     }
35                 },
36         methods: {
37             addStu() { 
38                 // 第一步: 獲得這個數組
39                 var studentArry = this.studentArry;
40                 // 第二步: 獲得id
41                 var newId = studentArry.length + 1;
42                 var newStu = {
43                     id: newId,
44                     name: "新生" + newId,
45                     age: 18
46                 }
47                 
48                 // studentArry.push(newStu);
49                 studentArry.unshift(newStu);
50             } 
51         }
52     }
53 </script>
54 
55 <style>
56 
57 </style>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM