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

Vue前端高效開(kāi)發(fā)之列表渲染指令

 更新時(shí)間:2021年06月27日 08:29:54   作者:PROBIE_  
這篇文章主要給大家介紹了關(guān)于Vue前端高效開(kāi)發(fā)之列表渲染指令的相關(guān)資料,vue.js 使用的是 v-for 指令來(lái)處理組件元素的循環(huán)迭代邏輯,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下

v-for指令

說(shuō)起列表不得不提起循環(huán),v-for指令便是可以在vue中實(shí)現(xiàn)循環(huán)的操作。

v-for指令是基于一個(gè)數(shù)組來(lái)重復(fù)渲染的指令,通常就用于顯示列表和表格。

語(yǔ)法:

<ul v-for="(鍵,值,索引) in 數(shù)組">
 <li>{{索引}}:{{值}}:{{鍵}}</li>
</ul>

例:

<body>
    <style>
        * {
            margin: 0px;
            padding: 0px;
        }
        
        ul {
            list-style: none;
        }
    </style>
    <!--遍歷數(shù)據(jù)-->
    <div id="app">
        <!--item:鍵-->
        <!--value:值-->
        <!--index:下標(biāo)-->
        <ul v-for="(item,value,index) in people">
            <li>{{index}}:{{value}}:{{item}}</li>
        </ul>
    </div>
    <script src="js/Vue.js"></script>
    <script>
        new Vue({
            el: "#app",
            data: {
                text: "我們的征途是星辰大海!",
                arr: ["瑪卡巴卡", "唔西迪西", "小點(diǎn)點(diǎn)", "湯姆布利多", "叮叮車(chē)"],
                people: {
                    id: 1,
                    name: "周潤(rùn)發(fā)",
                    age: 65
                }
            }
        })
    </script>
</body>

由例子可以看出,v-for指令不僅可以遍歷字符串,數(shù)組,還可以遍歷對(duì)象類(lèi)型,根據(jù)鍵值和索引,以列表或者表格形式顯示。

計(jì)算屬性

一般在項(xiàng)目開(kāi)發(fā)中,數(shù)據(jù)往往需要經(jīng)過(guò)一些處理,除了利用基本的表達(dá)式和過(guò)濾器外,還可以使用vue的計(jì)算屬性,優(yōu)化程序以及完成復(fù)雜計(jì)算。

例:實(shí)現(xiàn)模糊篩選以及增加和刪除。

首先通過(guò)v-for語(yǔ)句實(shí)現(xiàn)表格顯示數(shù)據(jù)

        <table class="table table-hover table-border">
            <tr class="info">
                <th>編號(hào)</th>
                <th>姓名</th>
                <th>年齡</th>
                <th>介紹</th>
            </tr>
            <tr v-for="item in lists">
                <td>{{item.id}}</td>
                <td>{{item.name}}</td>
                <td>{{item.age+"歲"}}</td>
                <td>{{item.show}}</td>
            </tr>
        </table>
    <script>
        new Vue({
            el: "#app",
            data: {
                "lists": [{
                    "id": 1,
                    "name": "張三",
                    "age": 18,
                    "show": "張三介紹"
                }, {
                    "id": 2,
                    "name": "李四",
                    "age": 19,
                    "show": "李四介紹"
                }, {
                    "id": 3,
                    "name": "王五",
                    "age": 20,
                    "show": "王五介紹"
                }, {
                    "id": 4,
                    "name": "趙六",
                    "age": 21,
                    "show": "趙六介紹"
                }, {
                    "id": 5,
                    "name": "孫八",
                    "age": 22,
                    "show": "孫八介紹"
                }]
            }
    </script>

使用計(jì)算屬性實(shí)現(xiàn)模糊查詢(xún)

        <p><input type="text" v-model="selectkey" placeholder="請(qǐng)輸入"></p>
            computed: {
                newlist: function() {
                    var _this = this;
                    return _this.lists.filter(function(val) {
                        return val.name.indexOf(_this.selectkey) != -1;
                    })
                }
            }

把計(jì)算屬性以函數(shù)形式寫(xiě)到computed選項(xiàng)中,將v-for語(yǔ)句遍歷的集合改為篩選后的newlist集合,通過(guò)判斷字符串中是否存在子字符串篩選lists集合中的數(shù)據(jù),再把篩選后的數(shù)據(jù)交給v-for遍歷顯示。

實(shí)現(xiàn)添加及刪除

        <p class="input-group">
            <span class="input-group-addon">編號(hào):</span>
            <input type="text" v-model="id" placeholder="請(qǐng)輸入編號(hào)" class="form-control">
        </p>
        <p class="input-group">
            <span class="input-group-addon">姓名:</span>
            <input type="text" v-model="name" placeholder="請(qǐng)輸入姓名" class="form-control">
        </p>
        <p class="input-group">
            <span class="input-group-addon">年齡:</span>
            <input type="text" v-model="age" placeholder="請(qǐng)輸入年齡" class="form-control">
        </p>
        <p class="input-group">
            <span class="input-group-addon">信息:</span>
            <input type="text" v-model="show" placeholder="請(qǐng)輸入信息" class="form-control">
        </p>
        <p class="input-group">
            <button @click="add()" class="btn btn-primary">添加信息</button>
        </p>
<td>
	<button v-on:click="dels(item.id)" class="btn btn-primary">刪除</button>
</td>
            methods: {
                add: function() {
                    var girl = {
                        "id": this.id,
                        "name": this.name,
                        "age": this.age,
                        "show": this.show
                    }
                    this.lists.push(girl);
                },
                dels: function(o) {
                    //刪除的是下標(biāo),刪除幾個(gè)
                    for (let i = 0; i < this.lists.length; i++) {
                        if (this.lists[i].id == o) {
                            this.lists.splice(i, 1);
                        }
                    }
                }
            }

通過(guò)methods綁定事件,添加兩個(gè)按鈕事件方法add和dels用于處理添加和刪除操作。

添加就是使用push方法添加,刪除這里的splice方法僅能通過(guò)下標(biāo)刪除,而傳過(guò)來(lái)的值是id所以這里為了確保正確性就需要循環(huán)判斷下標(biāo),進(jìn)行刪除操作。

這就是計(jì)算屬性。用于處理數(shù)據(jù)。

監(jiān)聽(tīng)屬性

vue除了計(jì)算屬性還提供了監(jiān)聽(tīng)屬性用于處理數(shù)據(jù),用于觀察數(shù)據(jù)變動(dòng)。

不同的是監(jiān)聽(tīng)屬性需要有兩個(gè)形參,一個(gè)是當(dāng)前值,一個(gè)是更新后的值。

例:

watch: {
        first: function (val) {
               this.full = val + ' ' + this.last
        },
        last: function (val) {
               this.full = this.first + ' ' + val
       }
} 

相比于監(jiān)聽(tīng)屬性,明顯計(jì)算屬性會(huì)優(yōu)于監(jiān)聽(tīng)屬性,所以在非特殊情況下,還是推薦優(yōu)先使用計(jì)算屬性。

總結(jié)

到此這篇關(guān)于Vue前端高效開(kāi)發(fā)之列表渲染指令的文章就介紹到這了,更多相關(guān)Vue列表渲染內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論