程序员scholar 程序员scholar
首页
  • Java 基础

    • JavaSE
    • JavaIO
    • JavaAPI速查
  • Java 高级

    • JUC
    • JVM
    • Java新特性
    • 设计模式
  • Web 开发

    • Servlet
    • Java网络编程
  • Web 标准

    • HTML
    • CSS
    • JavaScript
  • 前端框架

    • Vue2
    • Vue3
    • Vue3 + TS
    • 微信小程序
    • uni-app
  • 工具与库

    • jQuery
    • Ajax
    • Axios
    • Webpack
    • Vuex
    • WebSocket
    • 第三方登录
  • 后端与语言扩展

    • ES6
    • Typescript
    • node.js
  • Element-UI
  • Apache ECharts
  • 数据结构
  • HTTP协议
  • HTTPS协议
  • 计算机网络
  • Linux常用命令
  • Windows常用命令
  • SQL数据库

    • MySQL
    • MySQL速查
  • NoSQL数据库

    • Redis
    • ElasticSearch
  • 数据库

    • MyBatis
    • MyBatis-Plus
  • 消息中间件

    • RabbitMQ
  • 服务器

    • Nginx
  • Spring框架

    • Spring6
    • SpringMVC
    • SpringBoot
    • SpringSecurity
  • SpringCould微服务

    • SpringCloud基础
    • 微服务之DDD架构思想
  • 日常必备

    • 开发常用工具包
    • Hutoll工具包
    • IDEA常用配置
    • 开发笔记
    • 日常记录
    • 项目部署
    • 网站导航
    • 产品学习
    • 英语学习
  • 代码管理

    • Maven
    • Git教程
    • Git小乌龟教程
  • 运维工具

    • Docker
    • Jenkins
    • Kubernetes
  • 算法笔记

    • 算法思想
    • 刷题笔记
  • 面试问题常见

    • 十大经典排序算法
    • 面试常见问题集锦
关于
GitHub (opens new window)
首页
  • Java 基础

    • JavaSE
    • JavaIO
    • JavaAPI速查
  • Java 高级

    • JUC
    • JVM
    • Java新特性
    • 设计模式
  • Web 开发

    • Servlet
    • Java网络编程
  • Web 标准

    • HTML
    • CSS
    • JavaScript
  • 前端框架

    • Vue2
    • Vue3
    • Vue3 + TS
    • 微信小程序
    • uni-app
  • 工具与库

    • jQuery
    • Ajax
    • Axios
    • Webpack
    • Vuex
    • WebSocket
    • 第三方登录
  • 后端与语言扩展

    • ES6
    • Typescript
    • node.js
  • Element-UI
  • Apache ECharts
  • 数据结构
  • HTTP协议
  • HTTPS协议
  • 计算机网络
  • Linux常用命令
  • Windows常用命令
  • SQL数据库

    • MySQL
    • MySQL速查
  • NoSQL数据库

    • Redis
    • ElasticSearch
  • 数据库

    • MyBatis
    • MyBatis-Plus
  • 消息中间件

    • RabbitMQ
  • 服务器

    • Nginx
  • Spring框架

    • Spring6
    • SpringMVC
    • SpringBoot
    • SpringSecurity
  • SpringCould微服务

    • SpringCloud基础
    • 微服务之DDD架构思想
  • 日常必备

    • 开发常用工具包
    • Hutoll工具包
    • IDEA常用配置
    • 开发笔记
    • 日常记录
    • 项目部署
    • 网站导航
    • 产品学习
    • 英语学习
  • 代码管理

    • Maven
    • Git教程
    • Git小乌龟教程
  • 运维工具

    • Docker
    • Jenkins
    • Kubernetes
  • 算法笔记

    • 算法思想
    • 刷题笔记
  • 面试问题常见

    • 十大经典排序算法
    • 面试常见问题集锦
关于
GitHub (opens new window)
npm

(进入注册为作者充电)

  • Vue2

    • Vue简介
    • Vue 基础使用
    • Vue的基础指令
    • 过滤器(Filters)
    • 侦听器(Watch)
    • 计算属性(computed)
    • vue-cli
    • vue.config.js配置
    • Vue组件
    • 生命周期和数据共享
    • Vue 组件实例与数据代理 (this)
    • $refs 引用
    • 动态组件
      • 1. 什么是动态组件
      • 2. 如何实现动态组件渲染
      • 3. 使用 keep-alive 保持状态
      • 4. keep-alive 对应的生命周期函数
      • 5. keep-alive 的 include 属性
    • 插槽 (Slots)
    • 混入 (Mixin)
    • 自定义指令 (directives)
    • 插件 (Plugins)
    • 初识Vue-router
    • Vue-router的常见用法
  • Vue3

  • vue3 + TS 项目集成

  • Vue全家桶
  • Vue2
scholar
2024-07-31
目录

动态组件

# 动态组件

# 1. 什么是动态组件

动态组件指的是根据条件动态切换显示与隐藏的组件。这种方式允许在同一个位置根据不同的条件渲染不同的组件。

# 2. 如何实现动态组件渲染

Vue 提供了一个内置的 <component> 组件,用来实现动态组件的渲染。通过设置 <component> 组件的 is 属性,可以指定需要渲染的组件。<component> 组件会根据 is 属性的变化来动态切换不同的组件。

示例代码








 







 




















<template>
  <div id="app">
    <button @click="currentComponent = 'ComponentA'">显示组件A</button>
    <button @click="currentComponent = 'ComponentB'">显示组件B</button>
    <button @click="currentComponent = 'ComponentC'">显示组件C</button>

    <!-- 使用 <component> 组件实现动态组件渲染 -->
    <component :is="currentComponent"></component>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentComponent: 'ComponentA' // 默认显示组件A
    };
  },
  components: {
    ComponentA: {
      template: '<div>组件A</div>'
    },
    ComponentB: {
      template: '<div>组件B</div>'
    },
    ComponentC: {
      template: '<div>组件C</div>'
    }
  }
};
</script>

<style scoped>
/* 样式可以根据需要添加 */
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

在这个示例中,点击不同的按钮会动态切换显示不同的组件。<component> 组件的 is 属性值发生变化时,会重新渲染指定的组件。

实现原理

  • Vue 的 <component> 组件通过 is 属性动态决定渲染哪个组件。
  • 当 is 属性的值变化时,Vue 会卸载当前组件并加载新的组件。
  • <component> 并没有自身的渲染效果,它只是一个占位符,用来动态切换其他组件。

# 3. 使用 keep-alive 保持状态

默认情况下,切换动态组件时,组件的状态会被销毁。如果希望在组件切换后保持其状态,可以使用 Vue 内置的 <keep-alive> 组件。<keep-alive> 组件会缓存不活动的组件实例,而不是销毁它们。

示例代码








 
 
 







 




















<template>
  <div id="app">
    <button @click="currentComponent = 'ComponentA'">显示组件A</button>
    <button @click="currentComponent = 'ComponentB'">显示组件B</button>
    <button @click="currentComponent = 'ComponentC'">显示组件C</button>

    <!-- 使用 <keep-alive> 组件保持动态组件的状态 -->
    <keep-alive>
      <component :is="currentComponent"></component>
    </keep-alive>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentComponent: 'ComponentA' // 默认显示组件A
    };
  },
  components: {
    ComponentA: {
      template: '<div>组件A</div>'
    },
    ComponentB: {
      template: '<div>组件B</div>'
    },
    ComponentC: {
      template: '<div>组件C</div>'
    }
  }
};
</script>

<style scoped>
/* 样式可以根据需要添加 */
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

在这个示例中,组件切换后依然保持其状态,组件的状态不会被销毁。<keep-alive> 会缓存不活动的组件实例,使其在重新激活时保持之前的状态。

缓存状态原理

  • <keep-alive> 组件会缓存不活动的组件实例,而不是销毁它们。
  • 当组件被切换出去时,组件实例会被缓存而不会被销毁;当组件被切换回来时,组件实例会被重新激活,保持之前的状态。

# 4. keep-alive 对应的生命周期函数

当组件被缓存时,会自动触发组件的 deactivated 生命周期函数;当组件被激活时,会自动触发组件的 activated 生命周期函数。

示例代码








 
 
 







 





 
 
 
 
 
 



























<template>
  <div id="app">
    <button @click="currentComponent = 'ComponentA'">显示组件A</button>
    <button @click="currentComponent = 'ComponentB'">显示组件B</button>
    <button @click="currentComponent = 'ComponentC'">显示组件C</button>

    <!-- 使用 <keep-alive> 组件保持动态组件的状态 -->
    <keep-alive>
      <component :is="currentComponent"></component>
    </keep-alive>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentComponent: 'ComponentA' // 默认显示组件A
    };
  },
  components: {
    ComponentA: {
      template: '<div>组件A</div>',
      activated() {
        console.log('组件A被激活');
      },
      deactivated() {
        console.log('组件A被缓存');
      }
    },
    ComponentB: {
      template: '<div>组件B</div>',
      activated() {
        console.log('组件B被激活');
      },
      deactivated() {
        console.log('组件B被缓存');
      }
    },
    ComponentC: {
      template: '<div>组件C</div>',
      activated() {
        console.log('组件C被激活');
      },
      deactivated() {
        console.log('组件C被缓存');
      }
    }
  }
};
</script>

<style scoped>
/* 样式可以根据需要添加 */
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

在这个示例中,当组件被激活时会触发 activated 钩子函数,当组件被缓存时会触发 deactivated 钩子函数。

组件生命周期

  • 当组件被激活时,调用的是该组件内部的 activated 钩子函数,而不是父组件的 activated 钩子函数。
  • 当组件被缓存时,调用的是该组件内部的 deactivated 钩子函数。

# 5. keep-alive 的 include 属性

include 属性用来指定:只有名称匹配的组件会被缓存。多个组件名之间使用英文的逗号分隔。<keep-alive> 组件通过 include 属性可以有选择地缓存某些组件,而不缓存其他组件。

示例代码








 
 
 







 





 
 
 
 
 
 












 
 
 
 
 
 









<template>
  <div id="app">
    <button @click="currentComponent = 'ComponentA'">显示组件A</button>
    <button @click="currentComponent = 'ComponentB'">显示组件B</button>
    <button @click="currentComponent = 'ComponentC'">显示组件C</button>

    <!-- 使用 <keep-alive> 组件保持动态组件的状态,并指定缓存的组件 -->
    <keep-alive include="ComponentA,ComponentC">
      <component :is="currentComponent"></component>
    </keep-alive>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentComponent: 'ComponentA' // 默认显示组件A
    };
  },
  components: {
    ComponentA: {
      template: '<div>组件A</div>',
      activated() {
        console.log('组件A被激活');
      },
      deactivated() {
        console.log('组件A被缓存');
      }
    },
    ComponentB: {
      template: '<div>组件B</div>',
      activated() {
        console.log('组件B被激活');
      },
      deactivated() {
        console.log('组件B被缓存');
      }
    },
    ComponentC: {
      template: '<div>组件C</div>',
      activated() {
        console.log('组件C被激活');
      },
      deactivated() {
        console.log('组件C被缓存');
      }
    }
  }
};
</script>

<style scoped>
/* 样式可以根据需要添加 */
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

缓存体现

  • 只有 include 属性匹配的组件名称才会被缓存,其他组件切换出去后会被销毁。
  • 当缓存的组件切换回来时,组件状态保持不变。
编辑此页 (opens new window)
上次更新: 2025/01/30, 23:55:43
$refs 引用
插槽 (Slots)

← $refs 引用 插槽 (Slots)→

Theme by Vdoing | Copyright © 2019-2025 程序员scholar
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式