深入了解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 组件注册
Nov 20 Vue.js
Vue 的 v-model用法实例
Nov 23 Vue.js
实用的 vue tags 创建缓存导航的过程实现
Dec 03 Vue.js
在vue中使用inheritAttrs实现组件的扩展性介绍
Dec 07 Vue.js
Vue实现图书管理案例
Jan 20 Vue.js
vscode自定义vue模板的实现
Jan 27 Vue.js
详解vue身份认证管理和租户管理
May 25 Vue.js
vue Element-ui表格实现树形结构表格
Jun 07 Vue.js
SSM VUE Axios详解
Oct 05 Vue.js
Vue2.0搭建脚手架
Mar 13 Vue.js
vue如何实现关闭对话框后刷新列表
Apr 08 Vue.js
vue @ ~ 相对路径 路径别名设置方式
Jun 05 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
php 在文件指定行插入数据的代码
2010/05/08 PHP
php连接MSsql server的五种方法总结
2018/03/04 PHP
PHP count()函数讲解
2019/02/03 PHP
基于jQuery实现表格数据的动态添加与统计的代码
2011/01/31 Javascript
js防止表单重复提交的两种方法
2013/09/30 Javascript
解决jquery操作checkbox火狐下第二次无法勾选问题
2014/02/10 Javascript
通过隐藏iframe实现文件下载的js方法介绍
2014/02/26 Javascript
javascript中的事件代理初探
2014/03/08 Javascript
教你如何使用PHP输出中文JSON字符串
2014/05/22 Javascript
node.js中的dns.getServers方法使用说明
2014/12/08 Javascript
jQuery回到顶部的代码
2016/07/09 Javascript
javascript 取小数点后几位几种方法总结
2017/08/02 Javascript
官方推荐react-navigation的具体使用详解
2018/05/08 Javascript
[45:15]Optic vs VP 2018国际邀请赛淘汰赛BO3 第一场 8.24
2018/08/25 DOTA
Python中的defaultdict模块和namedtuple模块的简单入门指南
2015/04/01 Python
利用Tkinter和matplotlib两种方式画饼状图的实例
2017/11/06 Python
python 重定向获取真实url的方法
2018/05/11 Python
python如何查看微信消息撤回
2018/11/27 Python
python中partial()基础用法说明
2018/12/30 Python
python dict乱码如何解决
2020/06/07 Python
PyCharm上安装Package的实现(以pandas为例)
2020/09/18 Python
Python操作word文档插入图片和表格的实例演示
2020/10/25 Python
意大利珠宝店:Luxury Zone
2019/01/05 全球购物
美国购买舞会礼服网站:Couture Candy
2019/12/29 全球购物
JD Sports丹麦:英国领先的运动时尚零售商
2020/11/24 全球购物
小米官方旗舰店:Xiaomi
2020/08/07 全球购物
C#笔试题和英文面试题
2013/02/07 面试题
如果有两个类A,B,怎么样才能使A在发生一个事件的时候通知B
2016/03/12 面试题
上课迟到检讨书
2014/01/19 职场文书
幼儿园教师工作感言
2014/02/15 职场文书
小学一年级评语大全
2014/04/22 职场文书
作文批改评语大全
2014/04/23 职场文书
2014年幼儿园重阳节活动方案
2014/09/16 职场文书
2014年统计工作总结
2014/11/21 职场文书
《走遍天下书为侣》教学反思
2016/02/22 职场文书
python munch库的使用解析
2021/05/25 Python