Vue2.0中v-for迭代语法变化(keyindex)

Posted 寻坑者

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Vue2.0中v-for迭代语法变化(keyindex)相关的知识,希望对你有一定的参考价值。

语法发生了变化:http://blog.csdn.net/sinat_35512245/article/details/53966788

新数组语法 
value in arr 
(value, index) in arr

新对象语法 
value in obj (value, key) in obj 
(value, key, index) in obj

对 Vue.js——60分钟快速入门 的demo修改之后的源码为:

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="js/vue.js"></script>
    
    </head>
        
    <body>
        <div id="app">

            <fieldset>
                <legend>
                    Create New Person
                </legend>
                <div class="form-group">
                    <label>Name:</label>
                    <input type="text" v-model="newPerson.name"/>
                </div>
                <div class="form-group">
                    <label>Age:</label>
                    <input type="text" v-model="newPerson.age"/>
                </div>
                <div class="form-group">
                    <label>Sex:</label>
                    <select v-model="newPerson.sex">
                    <option value="Male">Male</option>
                    <option value="Female">Female</option>
                </select>
                </div>
                <div class="form-group">
                    <label></label>
                    <button @click="createPerson">Create</button>
                </div>
        </fieldset>
        <table>
            <thead>
                <tr>
            <th>Index</th> <th>Name</th> <th>Age</th> <th>Sex</th> <th>Delete</th> </tr> </thead> <tbody> <tr v-for="(person,index) in people"> <td>{{ index }}</td> <td>{{ person.name }}</td> <td>{{ person.age }}</td> <td>{{ person.sex }}</td> <td :class="\'text-center\'"><button @click="deletePerson(index)">Delete</button></td> </tr> </tbody> </table> </div> </body> <script src="js/vue.js"></script> <script> var vm = new Vue({ el: \'#app\', data: { newPerson: { name: \'\', age: 0, sex: \'Male\' }, people: [{ name: \'Jack\', age: 30, sex: \'Male\' }, { name: \'Bill\', age: 26, sex: \'Male\' }, { name: \'Tracy\', age: 22, sex: \'Female\' }, { name: \'Chris\', age: 36, sex: \'Male\' }] }, methods:{ createPerson: function(){ this.people.push(this.newPerson); // 添加完newPerson对象后,重置newPerson对象 this.newPerson = {name: \'\', age: 0, sex: \'Male\'} }, deletePerson: function(index){ // 删一个数组元素 this.people.splice(index,1); } } }) </script> </html>

 

以上是关于Vue2.0中v-for迭代语法变化(keyindex)的主要内容,如果未能解决你的问题,请参考以下文章

vue2.0和vue3.0中自定义指令(directive)的区别

vue2.0 之列表渲染-v-for

如何在Vue的v-for迭代中只调用一次方法?

vue2.0与vue3.0 循环渲染对比

v-for指令

Vue2.0的变化———vue2.0动画的变化vue-2.0路由的变化