動態(tài)組件與v-once指令-創(chuàng)新互聯(lián)

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="./vue.js"></script>
    <!-- <script src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script> -->
</head>
<body>
<div id="root">
    <child-one v-if="type == 'child-one'"></child-one>
    <child-two v-if="type == 'child-two'"></child-two>

    <button @click="handleButtonClick">change</button>
</div>
<script type="text/javascript">
    Vue.component("child-one", {
        template: `<div>child-one</div>`
    });

    Vue.component("child-two", {
        template: `<div>child-two</div>`
    });

    var vm = new Vue({
        el: "#root",
        data: {
            type: "child-one"
        },
        methods: {
            handleButtonClick: function() {
                this.type = (this.type == "child-one" ? "child-two" : "child-one");
            }
        }
    })
</script>
</body>
</html>

同樣的效果,使用動態(tài)組件(點擊切換會銷毀一個組件,創(chuàng)建另一個組件。很顯然,這樣對性能是有損耗的):

創(chuàng)新互聯(lián)擁有網(wǎng)站維護技術(shù)和項目管理團隊,建立的售前、實施和售后服務(wù)體系,為客戶提供定制化的成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、網(wǎng)站維護、四川服務(wù)器托管解決方案。為客戶網(wǎng)站安全和日常運維提供整體管家式外包優(yōu)質(zhì)服務(wù)。我們的網(wǎng)站維護服務(wù)覆蓋集團企業(yè)、上市公司、外企網(wǎng)站、商城系統(tǒng)網(wǎng)站開發(fā)、政府網(wǎng)站等各類型客戶群體,為全球1000多家企業(yè)提供全方位網(wǎng)站維護、服務(wù)器維護解決方案。
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="./vue.js"></script>
    <!-- <script src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script> -->
</head>
<body>
<div id="root">
    //當(dāng)然還可以用動態(tài)組件的方式(vue自帶的標(biāo)簽,它指的就是一個動態(tài)組件)。動態(tài)組件的意思是:它會根據(jù):is里面數(shù)據(jù)的變化自動加載不同的組件:
    <component :is="type"></component>

    <button @click="handleButtonClick">change</button>
</div>
<script type="text/javascript">
    Vue.component("child-one", {
        template: `<div>child-one</div>`
    });

    Vue.component("child-two", {
        template: `<div>child-two</div>`
    });

    var vm = new Vue({
        el: "#root",
        data: {
            type: "child-one"
        },
        methods: {
            handleButtonClick: function() {
                this.type = (this.type == "child-one" ? "child-two" : "child-one");
            }
        }
    })
</script>
</body>
</html>

為了解決頻繁銷毀-創(chuàng)建,可以用v-once(因為這個v-once,在切換的時候會把要銷毀的這個放內(nèi)存里了。也就是不需要創(chuàng)建了,直接從內(nèi)存里拿)

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="./vue.js"></script>
    <!-- <script src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script> -->
</head>
<body>
<div id="root">
    <!-- <child-one v-if="type == 'child-one'"></child-one>
    <child-two v-if="type == 'child-two'"></child-two> -->

    //當(dāng)然還可以用動態(tài)組件的方式(vue自帶的標(biāo)簽,它指的就是一個動態(tài)組件)。動態(tài)組件的意思是:它會根據(jù):is里面數(shù)據(jù)的變化自動加載不同的組件:
    <component :is="type"></component>

    <button @click="handleButtonClick">change</button>
</div>
<script type="text/javascript">
    Vue.component("child-one", {
        template: `<div v-once>child-one</div>`
    });

    Vue.component("child-two", {
        template: `<div v-once>child-two</div>`
    });

    var vm = new Vue({
        el: "#root",
        data: {
            type: "child-one"
        },
        methods: {
            handleButtonClick: function() {
                this.type = (this.type == "child-one" ? "child-two" : "child-one");
            }
        }
    })
</script>
</body>
</html>

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

文章名稱:動態(tài)組件與v-once指令-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://bm7419.com/article16/gggdg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、企業(yè)建站、Google、動態(tài)網(wǎng)站網(wǎng)頁設(shè)計公司、電子商務(wù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管