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

Vue使用$set和$delete操作對象屬性

 更新時間:2022年03月14日 11:50:02   作者:.NET開發(fā)菜鳥  
這篇文章介紹了Vue使用$set和$delete操作對象屬性的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、$set

在開始講解$set之前先看下面的一段代碼,實(shí)現(xiàn)的功能:當(dāng)點(diǎn)擊“添加”按鈕時,動態(tài)的給data里面的對象添加屬性和值,代碼示例如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>添加屬性</title>
    <!--引入vue.js-->
    <script src="node_modules/vue/dist/vue.js" ></script>
    <script>
        window.onload=function(){
            new Vue({
                el:'#app',// 2.0不允許掛載到html,body元素上
                data:{
                    info:{id:1,price:15,name:"套餐A"}
                },
                methods:{
                    add:function(){
                        // 給info對象添加msg屬性并賦值
                        this.info.msg="hello";
                    }
                }
            });
        }
    </script>
</head>
<body>
    <div id="app">
        {{info.msg}}
        <button @click="add">添加</button>
    </div>
</body>
</html>

先看看點(diǎn)擊按鈕之前的效果:

從截圖中可以看出這時info對象只有三個屬性,點(diǎn)擊“添加”按鈕刷新,然后在看看info對象的屬性,截圖如下:

可以看出這時info對象增加了msg屬性,但是界面上面沒有顯示出來msg屬性的值。即:

如果在實(shí)例創(chuàng)建之后添加新的屬性到實(shí)例上,不會觸發(fā)視圖更新。

這時就需要使用$set了。代碼示例如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>添加屬性</title>
    <!--引入vue.js-->
    <script src="node_modules/vue/dist/vue.js" ></script>
    <script>
        window.onload=function(){
            new Vue({
                el:'#app',// 2.0不允許掛載到html,body元素上
                data:{
                    info:{id:1,price:15,name:"套餐A"}
                },
                methods:{
                    add:function(){
                        // 給info對象添加msg屬性并賦值
                        //this.info.msg="hello";
                        this.$set(this.info,"msg","hello");
                    }
                }
            });
        }
    </script>
</head>
<body>
    <div id="app">
        {{info.msg}}
        <button @click="add">添加</button>
    </div>
</body>
</html>

效果:

可以看出:使用了$set之后視圖會被更新。

注意:如果是修改對象里面已經(jīng)存在的屬性,則直接修改即可

代碼示例如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>添加屬性</title>
    <!--引入vue.js-->
    <script src="node_modules/vue/dist/vue.js" ></script>
    <script>
        window.onload=function(){
            new Vue({
                el:'#app',// 2.0不允許掛載到html,body元素上
                data:{
                    info:{id:1,price:15,name:"套餐A"}
                },
                methods:{
                    add:function(){
                        // 給info對象添加msg屬性并賦值
                        //this.info.msg="hello";
                        this.$set(this.info,"msg","hello");
                    },
                    modify:function(){
                        this.info.name="套餐B";
                    }
                }
            });
        }
    </script>
</head>
<body>
    <div id="app">
        {{info.msg}}
        name值:{{info.name}}
        <button @click="add">添加</button>
        <button @click="modify">修改</button>
    </div>
</body>
</html>

效果:

二、$delete刪除對象屬性

可以使用$delete刪除對象里面的屬性,代碼示例如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>添加屬性</title>
    <!--引入vue.js-->
    <script src="node_modules/vue/dist/vue.js" ></script>
    <script>
        window.onload=function(){
            new Vue({
                el:'#app',// 2.0不允許掛載到html,body元素上
                data:{
                    info:{id:1,price:15,name:"套餐A"}
                },
                methods:{
                    add:function(){
                        // 給info對象添加msg屬性并賦值
                        //this.info.msg="hello";
                        this.$set(this.info,"msg","hello");
                    },
                    modify:function(){
                        this.info.name="套餐B";
                    },
                    del:function(){
                        // 刪除info對象里面的price屬性
                        this.$delete(this.info,"price");
                    }
                }
            });
        }
    </script>
</head>
<body>
    <div id="app">
        {{info.msg}}
        name值:{{info.name}}
        <button @click="add">添加</button>
        <button @click="modify">修改</button>
        <button @click="del">刪除</button>
    </div>
</body>
</html>

效果:

到此這篇關(guān)于Vue使用$set和$delete操作對象屬性的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論