深入了解Vue动态组件和异步组件


Posted in Vue.js onJanuary 26, 2021

1.动态组件

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <style>
		#app {
			font-size: 0
		}
		.dynamic-component-demo-tab-button {
			padding: 6px 10px;
			border-top-left-radius: 3px;
			border-top-right-radius: 3px;
			border: 1px solid #ccc;
			cursor: pointer;
			margin-bottom: -1px;
			margin-right: -1px;
			background: #f0f0f0;
		}
		.dynamic-component-demo-tab-button.dynamic-component-demo-active {
			background: #e0e0e0;
		}
		.dynamic-component-demo-tab-button:hover {
			background: #e0e0e0;
		}
		.dynamic-component-demo-posts-tab {
			display: flex;					
		}
		.dynamic-component-demo-tab {
			font-size: 1rem;
			border: 1px solid #ccc;
			padding: 10px;
		}
		.dynamic-component-demo-posts-sidebar {
			max-width: 40vw;
			margin: 0 !important;
			padding: 0 10px 0 0 !important;
			list-style-type: none;
			border-right: 1px solid #ccc;
			line-height: 1.6em;
		}
		.dynamic-component-demo-posts-sidebar li {
			white-space: nowrap;
			text-overflow: ellipsis;
			overflow: hidden;
			cursor: pointer;
		}
		.dynamic-component-demo-active {
			background: lightblue;
		}
		.dynamic-component-demo-post-container {
			padding-left: 10px;
		}
		.dynamic-component-demo-post > :first-child {
			margin-top: 0 !important;
			padding-top: 0 !important;
		}
 </style>
 <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
	<button v-for="tab in tabs" class="dynamic-component-demo-tab-button" 
		v-bind:class="{'dynamic-component-demo-active': tab === currentTab}" 
		@click="currentTab = tab">{{ tab }}</button>	
	<keep-alive>
		<component v-bind:is="currentTabComponent"></component>
	</keep-alive>
</div>
<script>
 Vue.component('tab-posts', {
		data: function(){
			return {
				posts: [
					{id: 1, title: 'Cat Ipsum', content: 'Cont wait for the storm to pass, ...'},
					{id: 2, title: 'Hipster Ipsum', content: 'Bushwick blue bottle scenester ...'},
					{id: 3, title: 'Cupcake Ipsum', content: 'Icing dessert souffle ...'},
				],
				selectedPost: null
			}
		},
 template: `<div class="dynamic-component-demo-posts-tab dynamic-component-demo-tab">
						<ul class="dynamic-component-demo-posts-sidebar">
							<li v-for="post in posts" 
								v-bind:key="post.id" 
								v-on:click="selectedPost = post" 
								v-bind:class="{'dynamic-component-demo-active': post===selectedPost}">
								{{ post.title }}
							</li>
						</ul>
						<div class="dynamic-component-demo-post-container">
							<div v-if="selectedPost" class="dynamic-component-demo-post">
								<h3>{{ selectedPost.title }}</h3>
								<div v-html="selectedPost.content"></div>
							</div>
							<strong v-else>
								Click on a blog title to the left to view it.
							</strong>
						</div>
					</div>`
 });
	
	Vue.component('tab-archive', {
		template: '<div class="dynamic-component-demo-tab">Archive component</div>'
	});

 new Vue({
 el: '#app',
		data: {
			currentTab: 'Posts',
			tabs: ['Posts', 'Archive']
		},
		computed: {
			currentTabComponent: function(){
				return 'tab-' + this.currentTab.toLowerCase()
			}
		}
 });
</script>
</body>
</html>

深入了解Vue动态组件和异步组件

在动态组件上使用keep-alive,可以在组件切换时保持组件的状态,避免了重复渲染的性能问题。

2.异步组件

Vue 允许你以一个工厂函数的方式定义你的组件,这个工厂函数会异步解析你的组件定义。

Vue.component('async-example', function (resolve, reject) {})

这里可以回顾一下 Vue.js — 组件基础。

我们使用通过webpack打包的Vue项目来介绍异步组件。

<!-- HelloWorld.vue -->
<template>
 <div>
 <h2 class="title">{{msg}}</h2>
 </div>
</template>

<script>
export default {
 data () {
 return {
 msg: 'Hello Vue!'
 }
 }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
 .title {
 padding: 5px;
 color: white;
 background: gray;
 }
</style>
<!-- App.vue -->
<template>
 <div id="app">
 <HelloWorld/>
 </div>
</template>

<script>
import HelloWorld from './components/HelloWorld'

export default {
 name: 'App',
 components: {
 HelloWorld
 }
}
</script>

<style>
</style>

我们把App.vue的<script>标签里面的内容改为:

export default {
 name: 'App',
 components: {
 HelloWorld: () => import('./components/HelloWorld')
 }
}

这样就实现了App组件异步加载HelloWorld组件的功能。

我们可以实现按需加载。

<!-- App.vue -->
<template>
 <div id="app">
 <button @click="show = true">Load Tooltip</button>
 <div v-if="show">
 <HelloWorld/>
 </div>
 </div>
</template>

<script>
export default {
 data: () => ({
 show: false
 }),
 components: {
 HelloWorld: () => import('./components/HelloWorld')
 }
}
</script>

<style>
</style>

这里的异步组件工厂函数也可以返回一个如下格式的对象:

const AsyncComponent = () => ({
 // 需要加载的组件 (应该是一个 `Promise` 对象)
 component: import('./MyComponent.vue'),
 // 异步组件加载时使用的组件
 loading: LoadingComponent,
 // 加载失败时使用的组件
 error: ErrorComponent,
 // 展示加载时组件的延时时间。默认值是 200 (毫秒)
 delay: 200,
 // 如果提供了超时时间且组件加载也超时了,
 // 则使用加载失败时使用的组件。默认值是:`Infinity`
 timeout: 3000
})

参考:

动态组件 & 异步组件 — Vue.js

以上就是深入了解Vue动态组件和异步组件的详细内容,更多关于Vue动态组件和异步组件的资料请关注三水点靠木其它相关文章!

Vue.js 相关文章推荐
vue 表单输入框不支持focus及blur事件的解决方案
Nov 17 Vue.js
vue 获取到数据但却渲染不到页面上的解决方法
Nov 19 Vue.js
Vue3配置axios跨域实现过程解析
Nov 25 Vue.js
Vue 实现一个简单的鼠标拖拽滚动效果插件
Dec 10 Vue.js
Vue使用鼠标在Canvas上绘制矩形
Dec 24 Vue.js
vue组件是如何解析及渲染的?
Jan 13 Vue.js
vue 中this.$set 动态绑定数据的案例讲解
Jan 29 Vue.js
vue3.0 项目搭建和使用流程
Mar 04 Vue.js
原生JS封装vue Tab切换效果
Apr 28 Vue.js
vue项目proxyTable配置和部署服务器
Apr 14 Vue.js
vue动态绑定style样式
Apr 20 Vue.js
使用vuex-persistedstate本地存储vuex
Apr 29 Vue.js
如何在 Vue 表单中处理图片
Jan 26 #Vue.js
Vue实现摇一摇功能(兼容ios13.3以上)
Jan 26 #Vue.js
Vue中的nextTick作用和几个简单的使用场景
Jan 25 #Vue.js
Vue使用Ref跨层级获取组件的步骤
Jan 25 #Vue.js
如何在Vue项目中添加接口监听遮罩
Jan 25 #Vue.js
使用vue3重构拼图游戏的实现示例
Jan 25 #Vue.js
vue 计算属性和侦听器的使用小结
Jan 25 #Vue.js
You might like
新浪新闻小偷
2006/10/09 PHP
php5 图片验证码实现代码
2009/12/11 PHP
php中mysql模块部分功能的简单封装
2011/09/30 PHP
php中的常用魔术方法总结
2013/08/02 PHP
跟我学Laravel之路由
2014/10/15 PHP
php的ddos攻击解决方法
2015/01/08 PHP
实例讲解PHP设计模式编程中的简单工厂模式
2016/02/29 PHP
PHP实现微信提现功能(微信商城)
2019/11/21 PHP
你可能不再需要JQUERY
2021/03/09 Javascript
js 绑定带参数的事件以及手动触发事件
2010/04/27 Javascript
浅析javascript闭包 实例分析
2010/12/25 Javascript
javascript各浏览器中option元素的表现差异
2011/04/07 Javascript
js修改原型的属性使用介绍
2014/01/26 Javascript
jquery序列化方法实例分析
2015/06/10 Javascript
jQuery插件datatables使用教程
2016/04/21 Javascript
基于js实现二级下拉联动
2016/12/17 Javascript
vue.js使用代理和使用Nginx来解决跨域的问题
2018/02/03 Javascript
vue-cli3.0 环境变量与模式配置方法
2018/11/08 Javascript
vue获取data数据改变前后的值方法
2019/11/07 Javascript
[00:43]2016完美“圣”典风云人物:单车宣传片
2016/12/02 DOTA
[00:52]黑暗之门更新 新英雄孽主驾临DOTA2
2016/08/24 DOTA
[01:36:17]DOTA2-DPC中国联赛 正赛 Ehome vs iG BO3 第一场 1月31日
2021/03/11 DOTA
用Python操作字符串之rindex()方法的使用
2015/05/19 Python
python数据结构之列表和元组的详解
2017/09/23 Python
浅谈python中字典append 到list 后值的改变问题
2018/05/04 Python
解决Pandas to_json()中文乱码,转化为json数组的问题
2018/05/10 Python
使用Python3 poplib模块删除服务器多天前的邮件实现代码
2020/04/24 Python
keras K.function获取某层的输出操作
2020/06/29 Python
Pytorch实验常用代码段汇总
2020/11/19 Python
实习销售业务员自我鉴定
2013/09/21 职场文书
公司活动方案范文
2014/03/06 职场文书
体育专业求职信
2014/07/16 职场文书
2014年度党员自我评议
2014/09/13 职场文书
百年校庆感言
2015/08/01 职场文书
评估“风险”创业计划的几大要点
2019/08/12 职场文书
用几道面试题来看JavaScript执行机制
2021/04/30 Javascript