keep-alive  传送门

  <keep-alive> 包裹动态组件时,会缓存不活动的组件实例,而不是销毁它们。和 <transition> 相似,<keep-alive> 是一个抽象组件:它自身不会渲染一个 DOM 元素,也不会出现在父组件链中。

  当组件在 <keep-alive> 内被切换,它的 activated 和 deactivated 这两个生命周期钩子函数将会被对应执行。

  主要用于保留组件状态或避免重新渲染

  

  include - 字符串或正则表达式。只有名称匹配的组件会被缓存
  exclude - 字符串或正则表达式。任何名称匹配的组件都不会被缓存
 

  Learn

    一、不使用<keep-alive>包裹动态组件

    二、使用<keep-alive>包裹动态组件

  目录结构

  

一、不使用<keep-alive>包裹动态组件

  此时组件A、B、C组件中的数会一直随机0~100且不重复

        <div id="GaryId">
<button @click="selectedName = 'my-component-a'"> a </button>
<button @click="selectedName = 'my-component-b'"> b </button>
<button @click="selectedName = 'my-component-c'"> c </button> <component :is="selectedName"></component> </div>
    "my-component-a":{
template:"<h1>A :{{num}}</h1>",
data(){
return{
num:Math.ceil(Math.random()*100)
}
}
},

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Gary</title>
</head>
<body>
<div id="GaryId">
<button @click="selectedName = 'my-component-a'"> a </button>
<button @click="selectedName = 'my-component-b'"> b </button>
<button @click="selectedName = 'my-component-c'"> c </button> <component :is="selectedName"></component> </div>
</body> <script type="text/javascript" src="../js/vue.js" ></script>
<script type="text/javascript"> new Vue({
data:{
selectedName:'my-component-a'
},
components:{
"my-component-a":{
template:"<h1>A :{{num}}</h1>",
data(){
return{
num:Math.ceil(Math.random()*100)
}
}
},
"my-component-b":{
template:"<h1>B :{{num}}</h1>",
data(){
return{
num:Math.ceil(Math.random()*100)
}
}
},
"my-component-c":{
template:"<h1>C :{{num}}</h1>",
data(){
return{
num:Math.ceil(Math.random()*100)
}
}
}
}
}).$mount("#GaryId");
</script>
</html>

Gary_dynamic_component.html

二、使用<keep-alive>包裹动态组件

  此时组件A、B、C组件中的数会第一次会随机出现,随后保存到缓存中,第二次再点击的时候它们会读取缓存中的数

        <div id="GaryId">
<button @click="selectedName = 'my-component-a'"> a </button>
<button @click="selectedName = 'my-component-b'"> b </button>
<button @click="selectedName = 'my-component-c'"> c </button> <keep-alive>
<component :is="selectedName"></component>
</keep-alive>
</div>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Gary</title>
</head>
<body>
<div id="GaryId">
<button @click="selectedName = 'my-component-a'"> a </button>
<button @click="selectedName = 'my-component-b'"> b </button>
<button @click="selectedName = 'my-component-c'"> c </button> <keep-alive>
<component :is="selectedName"></component>
</keep-alive>
</div>
</body> <script type="text/javascript" src="../js/vue.js" ></script>
<script type="text/javascript"> new Vue({
data:{
selectedName:'my-component-a'
},
components:{
"my-component-a":{
template:"<h1>A :{{num}}</h1>",
data(){
return{
num:Math.ceil(Math.random()*100)
}
}
},
"my-component-b":{
template:"<h1>B :{{num}}</h1>",
data(){
return{
num:Math.ceil(Math.random()*100)
}
}
},
"my-component-c":{
template:"<h1>C :{{num}}</h1>",
data(){
return{
num:Math.ceil(Math.random()*100)
}
}
}
}
}).$mount("#GaryId");
</script>
</html>

Gary_dynamic_component.html

  当只想缓存A组件

            <keep-alive include="my-component-a">
<component :is="selectedName"></component>
</keep-alive>

  当想缓存A组件和B组件时候

            <keep-alive :include="['my-component-a','my-component-b']">
<component :is="selectedName"></component>
</keep-alive>

  排除缓存A组件和B组件的时候

            <keep-alive :exclude="['my-component-a','my-component-b']">
<component :is="selectedName"></component>
</keep-alive>

Vue_(组件通讯)动态组件结合keep-alive的更多相关文章

  1. Vue_(组件通讯)动态组件

    动态组件 传送门 在一个元素上挂载多个组件,根据不同状态进行切换的时候,可以使用动态组件 动态组件的使用:需要使用内置组件<component></component>,根据 ...

  2. Vue_(组件通讯)子组件向父组件传值

    Vue组件 传送门 子组件向父组件传值:子组件通过$.emit()方法以事件形式向父组件发送消息传值: 使用步骤: 1.定义组件:现有自定义组件com-a.com-b,com-a是com-b的父组件: ...

  3. Vue_(组件通讯)父组件向子组件传值

    Vue组件 传送门 父组件向子组件传值:父组件通过属性向下传值的方式和子组件通信: 使用步骤: 1.定义组件:现有自定义组件com-a.com-b,com-a是com-b的父组件 2.准备获取数据:c ...

  4. Vue_(组件通讯)父子组件简单关系

    Vue组件 传送门 在Vue的组件内也可以定义组件,这种关系成为父子组件的关系 如果在一个Vue实例中定义了component-a,然后在component-a中定义了component-b,那他们的 ...

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

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

  6. Vue两种组件类型介绍:递归组件和动态组件

    一递归组件 递归组件的特性就是可以在自己的template模板中调用自己本身.值得注意的它必须设置name属性. // 递归组件 recursive.vue <template> < ...

  7. [Vue]组件——实现动态组件:keep-alive的使用

    1.在app.vue中用一个 <keep-alive> 元素将其动态组件包裹起来: keepAlive为true时,第一次被创建的时候缓存下来,为false时,不会缓存 <keep- ...

  8. Vue 组件4 动态组件

    动态组件 通过使用保留的<component>元素,动态的绑定到它的is特性,我们让多个组件同时使用同一个挂载点,并动态切换: var vm = new Vue({ el: '#examp ...

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

    作者 | Jeskson 来源 | 达达前端小酒馆 v-model双向绑定 创建双向数据绑定,v-model指令用来在input,select,checkbox,radio等表单控件.v-model指 ...

随机推荐

  1. webpack编写一个plugin插件

    插件向第三方开发者提供了 webpack 引擎中完整的能力.使用阶段式的构建回调,开发者可以引入它们自己的行为到 webpack 构建流程中.创建插件比创建 loader 更加高级,因为你将需要理解一 ...

  2. C#中判断文件夹或文件是否存在的方法

    一.根据虚拟路径获取文件物理路径: string savePath = Server.MapPath("~/Uploads/RemoteDatum/"); 二.判断文件夹是否存在 ...

  3. wpf之二进制资源

    一.当需要添加图片.音频.视屏的资源到wpf项目里是,可以直接把文件添加到项目里 右击add->existing item. 1.如果想将外部文件编异常目标成为二进制资源,在文件的属性窗口 Bu ...

  4. xpath下载

    1.下载地址,版本号为 2.0.2: 链接: https://pan.baidu.com/s/1GXPm1kMENXhOkefKcEQnlA 密码: 8wwv 2.安装 1).在chrome右上角找到 ...

  5. php-fpm三种运行模式

    php-fpm配置 配置文件:php-fpm.conf 开启慢日志功能的: slowlog = /usr/local/var/log/php-fpm.log.slowrequest_slowlog_t ...

  6. vue的数据代理

    1. vue数据代理: data对象的所有属性的操作(读/写)由vm对象来代理操作2. 好处: 通过vm对象就可以方便的操作data中的数据3. 实现: 1). 通过Object.defineProp ...

  7. jQuery实现照片墙,附步骤详解

    现在一直使用vue写项目,发现之前的js都很生疏了,写个小demo练下手,看一下最终效果展示 功能点:点击添加图片随机添加一张图片,图片可以拖动,可以点击删除 技能点: 主要使用了jQuery的一些方 ...

  8. Apache Shiro漏洞复现

    利用burp dns进行检测,脚本如下: import sys import uuid import base64 import subprocess from Crypto.Cipher impor ...

  9. Oracle【select from 语句】

    Oracle[select from  语句] 1.select基本功能介绍1)投影操作:结果集是源表中的部分“列”2)选择操作:结果集是源表中的部分“行”3)选择操作+投影操作:结果集是源表中的部分 ...

  10. qunee 缩略图

    <!DOCTYPE html> <html> <head> <title>Hello Qunee for HTML5</title> < ...