动态组件与 v-once 指令

<!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>

同样的效果,使用动态组件(点击切换会销毁一个组件,创建另一个组件。很显然,这样对性能是有损耗的):

<!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">
    //当然还可以用动态组件的方式(vue自带的标签,它指的就是一个动态组件)。动态组件的意思是:它会根据:is里面数据的变化自动加载不同的组件:
    <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> 

为了解决频繁销毁-创建,可以用v-once(因为这个v-once,在切换的时候会把要销毁的这个放内存里了。也就是不需要创建了,直接从内存里拿)

<!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> -->

    //当然还可以用动态组件的方式(vue自带的标签,它指的就是一个动态组件)。动态组件的意思是:它会根据:is里面数据的变化自动加载不同的组件:
    <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> 

原文地址:https://blog.51cto.com/5660061/2419544

时间: 2024-07-31 08:55:10

动态组件与 v-once 指令的相关文章

vue_动态组件与v-once指令

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <bod

angular2 学习笔记 ( Dynamic Component 动态组件)

一样这一篇最要讲概念而已. refer : http://blog.rangle.io/dynamically-creating-components-with-angular-2/ (例子)https://www.ag-grid.com/ag-grid-angular-aot-dynamic-components/ (动态 entryComponents) http://stackoverflow.com/questions/40106480/what-are-projectable-node

0810 vue 创建组件 模板 动态组件 传值

lesson10 1.demo    vue样本 <body> <div id="myApp"> </div> </body> <script> new Vue({ el:"#myApp", data:{}, methods:{}, computed:{}, filters:{} }) </script> 2.案例: 模拟百度搜索框 <!DOCTYPE html> <html

Vue组件的操作-自定义组件,动态组件,递归组件

作者 | Jeskson 来源 | 达达前端小酒馆 v-model双向绑定 创建双向数据绑定,v-model指令用来在input,select,checkbox,radio等表单控件.v-model指令在内部使用不同的属性为不同的输入元素抛出不同的事件. v-mdel指令实现数据的双向绑定: <div> 用户名:<input type="text" v-model="name"> </div> 输入用户名是:{{name}} &l

Hibernate学习---第五节:普通组件和动态组件

一.普通组件映射配置 1.创建组件类,代码如下: package learn.hibernate.bean; /** * 组件类 */ public class Phones { private String companyPhone; private String homePhone; private String personalPhone; public Phones() { } public Phones(String companyPhone, String homePhone, St

vue2入坑随记(二) -- 自定义动态组件

学习了Vue全家桶和一些UI基本够用了,但是用元素的方式使用组件还是不够灵活,比如我们需要通过js代码直接调用组件,而不是每次在页面上通过属性去控制组件的表现.下面讲一下如何定义动态组件. Vue.extend 思路就是拿到组件的构造函数,这样我们就可以new了.而Vue.extend可以做到:https://cn.vuejs.org/v2/api/#Vue-extend // 创建构造器 var Profile = Vue.extend({ template: '<p>{{firstName

Vue动态组件

前面的话 让多个组件使用同一个挂载点,并动态切换,这就是动态组件.本文将详细介绍Vue动态组件 概述 通过使用保留的 <component> 元素,动态地绑定到它的 is 特性,可以实现动态组件 <div id="example"> <button @click="change">切换页面</button> <component :is="currentView"></compon

C++ 类的动态组件化技术

序言: N年前,我们曾在软件开发上出现了这样的困惑,用VC开发COM组件过于复杂,用VB开发COM组件发现效率低,而且不能实现面向对象的很多特性,例如,继承,多态等.更况且如何快速封装利用历史遗留的大量C++代码本身就是一个大的难题. 当时,开发小组的成员通过共同努力,摸索了一套C++类的动态组件化技术,很好的解决了以上的问题,通过这个技术,我们继承了大量的C++代码,同时使这些C++程序以COM+组件的形式得以新生.通过这几年在实际应用中的考验,这个技术是成熟可靠的. 也许新的系统大多数都完全

Oracle动态性能表-V$SESSION_WAIT,V$SESSION_EVENT

(1)-V$SESSION_WAIT 这是一个寻找性能瓶颈的关键视图.它提供了任何情况下session在数据库中当前正在等待什么(如果session当前什么也没在做,则显示它最后的等待事件).当系统存在性能问题时,本视图可以做为一个起点指明探寻问题的方向. V$SESSION_WAIT中,每一个连接到实例的session都对应一条记录. V$SESSION_WAIT中的常用列 l         SID: session标识 l         EVENT: session当前等待的事件,或者最