Vue动态组件实例解析
发布时间 - 2026-01-11 02:52:32 点击率:次前面的话

让多个组件使用同一个挂载点,并动态切换,这就是动态组件。本文将详细介绍Vue动态组件
概述
通过使用保留的 <component> 元素,动态地绑定到它的 is 特性,可以实现动态组件
<div id="example">
<button @click="change">切换页面</button>
<component :is="currentView"></component>
</div>
<script>
var home = {template:'<div>我是主页</div>'};
var post = {template:'<div>我是提交页</div>'};
var archive = {template:'<div>我是存档页</div>'};
new Vue({
el: '#example',
components: {
home,
post,
archive,
},
data:{
index:0,
arr:['home','post','archive'],
},
computed:{
currentView(){
return this.arr[this.index];
}
},
methods:{
change(){
this.index = (++this.index)%3;
}
}
})
</script>
也可以直接绑定到组件对象上
<div id="example">
<button @click="change">切换页面</button>
<component :is="currentView"></component>
</div>
<script>
new Vue({
el: '#example',
data:{
index:0,
arr:[
{template:`<div>我是主页</div>`},
{template:`<div>我是提交页</div>`},
{template:`<div>我是存档页</div>`}
],
},
computed:{
currentView(){
return this.arr[this.index];
}
},
methods:{
change(){
this.index = (++this.index)%3;
}
}
})
</script>
缓存
<keep-alive> 包裹动态组件时,会缓存不活动的组件实例,而不是销毁它们。和 <transition> 相似,<keep-alive> 是一个抽象组件:它自身不会渲染一个 DOM 元素,也不会出现在父组件链中
【基础用法】
<div id="example">
<button @click="change">切换页面</button>
<keep-alive>
<component :is="currentView"></component>
</keep-alive>
</div>
<script>
new Vue({
el: '#example',
data:{
index:0,
arr:[
{template:`<div>我是主页</div>`},
{template:`<div>我是提交页</div>`},
{template:`<div>我是存档页</div>`}
],
},
computed:{
currentView(){
return this.arr[this.index];
}
},
methods:{
change(){
let len = this.arr.length;
this.index = (++this.index)% len;
}
}
})
</script>
【条件判断】
如果有多个条件性的子元素,<keep-alive> 要求同时只有一个子元素被渲染
<div id="example">
<button @click="change">切换页面</button>
<keep-alive>
<home v-if="index===0"></home>
<posts v-else-if="index===1"></posts>
<archive v-else></archive>
</keep-alive>
</div>
<script>
new Vue({
el: '#example',
components:{
home:{template:`<div>我是主页</div>`},
posts:{template:`<div>我是提交页</div>`},
archive:{template:`<div>我是存档页</div>`},
},
data:{
index:0,
},
methods:{
change(){
let len = Object.keys(this.$options.components).length;
this.index = (++this.index)%len;
}
}
})
</script>
【activated 和 deactivated】
activated 和 deactivated 在 <keep-alive> 树内的所有嵌套组件中触发
<div id="example">
<button @click="change">切换页面</button>
<keep-alive>
<component :is="currentView" @pass-data="getData"></component>
</keep-alive>
<p>{{msg}}</p>
</div>
<script>
new Vue({
el: '#example',
data:{
index:0,
msg:'',
arr:[
{
template:`<div>我是主页</div>`,
activated(){
this.$emit('pass-data','主页被添加');
},
deactivated(){
this.$emit('pass-data','主页被移除');
},
},
{template:`<div>我是提交页</div>`},
{template:`<div>我是存档页</div>`}
],
},
computed:{
currentView(){
return this.arr[this.index];
}
},
methods:{
change(){
var len = this.arr.length;
this.index = (++this.index)% len;
},
getData(value){
this.msg = value;
setTimeout(()=>{
this.msg = '';
},500)
}
}
})
</script>
【include和exclude】
include 和 exclude 属性允许组件有条件地缓存。二者都可以用逗号分隔字符串、正则表达式或一个数组来表示
<!-- 逗号分隔字符串 --> <keep-alive include="a,b"> <component :is="view"></component> </keep-alive> <!-- 正则表达式 (使用 v-bind) --> <keep-alive :include="/a|b/"> <component :is="view"></component> </keep-alive> <!-- Array (use v-bind) --> <keep-alive :include="['a', 'b']"> <component :is="view"></component> </keep-alive>
匹配首先检查组件自身的 name 选项,如果 name 选项不可用,则匹配它的局部注册名称(父组件 components 选项的键值)。匿名组件不能被匹配
<keep-alive include="home,archive"> <component :is="currentView"></component> </keep-alive>
上面的代码,表示只缓存home和archive,不缓存posts
<div id="example">
<button @click="change">切换页面</button>
<keep-alive include="home,archive">
<component :is="currentView"></component>
</keep-alive>
</div>
<script src="https://unpkg.com/vue"></script>
<script>
new Vue({
el: '#example',
data:{
index:0,
arr:[
{name:'home',template:`<div>我是主页</div>`},
{name:'posts',template:`<div>我是提交页</div>`},
{name:'archive',template:`<div>我是存档页</div>`}
],
},
computed:{
currentView(){
return this.arr[this.index];
}
},
methods:{
change(){
var len = this.arr.length;
this.index = (++this.index)% len;
},
}
})
</script>
总结
以上所述是小编给大家介绍的Vue动态组件实例解析,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
# Vue动态组件
# Vue动态组件与异步组件实例详解
# vue动态子组件的两种实现方式
# vue 动态创建组件的两种方法
# Vue高级用法实例教程之动态组件
# Vue 自定义动态组件实例详解
# 前端架构vue动态组件使用基础教程
# 我是
# 多个
# 绑定
# 小编
# 是一个
# 这就是
# 出现在
# 可以用
# 给大家
# 可以直接
# 正则表达式
# 只有一个
# 详细介绍
# 可以实现
# 所述
# 给我留言
# 不可用
# 键值
# 移除
# 而不是
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Laravel如何设置定时任务(Cron Job)_Laravel调度器与任务计划配置
javascript中对象的定义、使用以及对象和原型链操作小结
Windows驱动无法加载错误解决方法_驱动签名验证失败处理步骤
Laravel如何处理JSON字段_Eloquent原生JSON字段类型操作教程
智能起名网站制作软件有哪些,制作logo的软件?
Python文件操作最佳实践_稳定性说明【指导】
html5怎么画眼睛_HT5用Canvas或SVG画眼球瞳孔加JS控制动态【绘制】
企业在线网站设计制作流程,想建设一个属于自己的企业网站,该如何去做?
C语言设计一个闪闪的圣诞树
如何在VPS电脑上快速搭建网站?
Laravel如何理解并使用服务容器(Service Container)_Laravel依赖注入与容器绑定说明
奇安信“盘古石”团队突破 iOS 26.1 提权
如何快速生成专业多端适配建站电话?
详解Oracle修改字段类型方法总结
韩国服务器如何优化跨境访问实现高效连接?
Python并发异常传播_错误处理解析【教程】
Laravel如何使用Collections进行数据处理?(实用方法示例)
php做exe能调用系统命令吗_执行cmd指令实现方式【详解】
大连网站制作公司哪家好一点,大连买房网站哪个好?
成都网站制作公司哪家好,四川省职工服务网是做什么用?
悟空识字怎么关闭自动续费_悟空识字取消会员自动扣费步骤
Laravel如何实现多级无限分类_Laravel递归模型关联与树状数据输出【方法】
制作网站软件推荐手机版,如何制作属于自己的手机网站app应用?
如何在阿里云购买域名并搭建网站?
Android中Textview和图片同行显示(文字超出用省略号,图片自动靠右边)
HTML5空格和nbsp有啥关系_nbsp的作用及使用场景【说明】
Swift中switch语句区间和元组模式匹配
Laravel怎么实现前端Toast弹窗提示_Laravel Session闪存数据Flash传递给前端【方法】
Laravel如何配置中间件Middleware_Laravel自定义中间件拦截请求与权限校验【步骤】
如何快速启动建站代理加盟业务?
Laravel如何设置自定义的日志文件名_Laravel根据日期或用户ID生成动态日志【技巧】
高防服务器:AI智能防御DDoS攻击与数据安全保障
黑客如何利用漏洞与弱口令入侵网站服务器?
高性能网站服务器部署指南:稳定运行与安全配置优化方案
Laravel如何使用模型观察者?(Observer代码示例)
如何在 Pandas 中基于一列条件计算另一列的分组均值
Laravel Blade模板引擎语法_Laravel Blade布局继承用法
油猴 教程,油猴搜脚本为什么会网页无法显示?
EditPlus中的正则表达式 实战(1)
如何在万网自助建站平台快速创建网站?
成都品牌网站制作公司,成都营业执照年报网上怎么办理?
Laravel Debugbar怎么安装_Laravel调试工具栏配置指南
Laravel如何创建自定义中间件?(Middleware代码示例)
如何在阿里云ECS服务器部署织梦CMS网站?
如何在橙子建站上传落地页?操作指南详解
Laravel怎么在Controller之外的地方验证数据
个人网站制作流程图片大全,个人网站如何注销?
简单实现Android验证码
胶州企业网站制作公司,青岛石头网络科技有限公司怎么样?
如何在企业微信快速生成手机电脑官网?

