Vue - 动态组件 & 异步组件
动态组件
<div id="app">
	<components :is="com[2]"></components>
	<components :is="com[1]"></components>
	<components :is="com[0]"></components>
</ul>
</div>
<script>
var comA = {
	template: '<p>永远相信美好的事情即将发生</p>'
}
var comB = {
	template: '<p>javascript学个简单的精通</p>'
}
var comC = {
	template: '<p>学个php都没时间 妈的</p>'
}
new Vue({
    el: '#app',
    components: {
    	comA,
    	comB,
    	comC
    },
    data: {
       	com: ['com-a', 'com-b', 'com-c']
    }
})
</script>

动态组件 keep-alive
当在这些组件之间切换的时候,你有时会想保持这些组件的状态,以避免反复重渲染导致的性能问题。
重新创建动态组件的行为通常是非常有用的,但是在这个案例中,我们更希望那些标签的组件实例能够被在它们第一次被创建的时候缓存下来。为了解决这个问题,我们可以用一个 <keep-alive> 元素将其动态组件包裹起来。
<style>
.tab-button {
  padding: 6px 10px;
  border-top-left-radius: 3px;
  border-top-right-radius: 3px;
  border: 1px solid #ccc;
  cursor: pointer;
  background: #f0f0f0;
  margin-bottom: -1px;
  margin-right: -1px;
}
.tab-button:hover {
  background: #e0e0e0;
}
.tab-button.active {
  background: #e0e0e0;
}
.tab {
  border: 1px solid #ccc;
  padding: 10px;
}
.posts-tab {
  display: flex;
}
.posts-sidebar {
  max-width: 40vw;
  margin: 0;
  padding: 0 10px 0 0;
  list-style-type: none;
  border-right: 1px solid #ccc;
}
.posts-sidebar li {
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
  cursor: pointer;
}
.posts-sidebar li:hover {
  background: #eee;
}
.posts-sidebar li.selected {
  background: lightblue;
}
.selected-post-container {
  padding-left: 10px;
}
.selected-post > :first-child {
  margin-top: 0;
  padding-top: 0;
}
</style>
<div id="app">
	<button
		v-for="tab in tabs"
		v-bind:key="tab"
		v-bind:class="['tab-button', {active: currentTab === tab}]"
		v-on:click="currentTab = tab"
	>{{tab}}</button>
	<keep-alive>
		<components
			v-bind:is="currentTabComponent"
			class="tab"
		></components>
	</keep-alive>
</div>
<script>
var Posts = {
	data: function(){
		return{
			posts: [
				{
					id: 1,
					title: 'Cat Ipsum',
					content: '<p>抽时间学PHP啊啊啊啊</p>'
				},
				{
					id: 2,
					title: 'Hipster Ipsum',
					content: '<p>javascript 简单的精通</p>'
				},
				{
					id: 3,
					title: 'Cupcake Ipsum',
					content: '<p>时间啊 我的朋友 让我们好好相处 珍惜时光 好不好</p>'
				}
			],
			selectedPost: true
		}
	},
	template: `
		<div class="posts-tab">
			<ul class="posts-sidebar">
				<li
					v-for="post in posts"
					v-bind:class="{selected: post === selectedPost}"
					@click="selectedPost = post"
				>{{post.title}}</li>
			</ul>
			<div class="selected-post-container">
				<div v-if="selectedPost">
					<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>
	`
}
var Archive = {
	template: '<p>javascript学个简单的精通</p>'
}
new Vue({
    el: '#app',
    components: {
    	Posts,
    	Archive
    },
    data: {
       	tabs: ['Posts', 'Archive'],
       	// 默认是显示第一个组件
       	currentTab: 'Posts'
    },
    computed: {
    	currentTabComponent: function(){
    		// 转换为小写
    		return this.currentTab.toLowerCase()
    	}
    }
})
</script>

## 异步组件
XXXXXX
Vue - 动态组件 & 异步组件的更多相关文章
- Vue动态注册异步组件(非同一个工程的组件)
		
前言:最近在掘金逛的时候,无意中看到前滴滴前端架构黄轶大佬,看到了大佬分享的一篇博客滴滴 webapp 5.0 Vue 2.0 重构经验分享 ,对于其中第5个问题(异步加载的业务线组件,如何动态注册? ...
 - vue动态加载组件
		
vue动态加载组件,可以使用以下方式 <component :is="propertyname" v-for="tab in tabs"></ ...
 - vue深入了解组件——动态组件&异步组件
		
一.在动态组件上使用 keep-alive 我们之前曾经在一个多标签的界面中使用 is 特性来切换不同的组件: <component v-bind:is="currentTabComp ...
 - vue组件---动态组件&异步组件
		
(1)在动态组件上使用keep-alive 之前曾经在一个多标签的界面中使用 is 特性来切换不同的组件.接下来简单回顾下 <component>元素是vue 里面的一个内置组件.在里面使 ...
 - Vue动态组件&异步组件
		
在动态组件上使用keep-alive 我们之前曾经在一个多标签的界面中使用is特性来切换不同的组件: Vue.js的动态组件模板 <component v-bind:is="curre ...
 - 七、vue语法补充二(动态组件 & 异步组件、访问元素 & 组件、混入)
		
1..sync 修饰符 2.3.0+ 新增 vue 修饰符sync的功能是:当一个子组件改变了一个 prop 的值时,这个变化也会同步到父组件中所绑定.类似于v-model的效果 例子: this.$ ...
 - 学习笔记:Vue——动态组件&异步组件
		
动态组件 01.在动态组件上使用keep-alive,保持组件的状态,以避免反复重渲染导致的性能问题. <!-- 失活的组件将会被缓存!--> <keep-alive> < ...
 - 深入了解组件- -- 动态组件 & 异步组件
		
gitHub地址:https://github.com/huangpna/vue_learn/example里面的lesson11 一 在动态组件上使用keep-alive 在这之前我们已经有学习过用 ...
 - vue -- 动态加载组件  (tap 栏效果)
		
在 vue 中,实现 Tab 切换主要有三种方式:使用动态组件,使用 vue-router 路由,使用第三方插件. 因为这次完成的功能只是简单切换组件,再则觉得使用路由切换需要改变地址略微麻烦,所以使 ...
 
随机推荐
- css权重及计算
			
一.一般而言:!important--->行间样式--->id--->class | 属性--->标签选择器--->通配符 二.权重值 !important ...
 - Spark学习之路 (一)Spark初识 [转]
			
官网介绍 什么是Spark 官网地址:http://spark.apache.org/ Apache Spark™是用于大规模数据处理的统一分析引擎. 从右侧最后一条新闻看,Spark也用于AI人工智 ...
 - python语言基础3
			
一:python函数 是组织好的,可重复使用的,用来实现单一,或相关联功能的代码块.以前使用过的一些Python提供的内建函数,如print().max(4,18).min(100,50).当然我们自 ...
 - Educational Codeforces Round 81 (Rated for Div. 2) 题解
			
过了n天补的题解:D AB就不用说了 C. Obtain The String 思路挺简单的,就是贪心,但是直接贪心的复杂度是O(|s|*|t|),会超时,所以需要用到序列自动机 虽然名字很高端但是就 ...
 - babel 的简单使用
			
之前在项目中使用.balelrc文件,但是一直不知道具体怎么使用,就知道可以将es6语法转码为es5语法. 今天就简单的做个例子,也算是记录一下困扰了好久的问题. 转码步骤: 首先在项目的目录中安装B ...
 - angular2 给下拉框动态赋值
			
html页中 其中aab是从后台获取的动态数据
 - 菜鸟教程  Missing parentheses in call to 'print'
			
个人博客 地址:http://www.wenhaofan.com/article/20180618180327 >>> print "hello" SyntaxE ...
 - 安装python3.7.4时报错:Service Pack 1 is required to continue installation
			
python3.7.4安装失败:Service Pack 1 is required to continue installation 解决办法: 点击报错页面中的“log file”,日志最后一行显 ...
 - GitBook的使用方式,快速创建网页文档
			
环境需求:node npm 得装好 ----------------------------------- 我一开始不知道得先装gitbook-cli 先执行了 npm install gitbook ...
 - Druid连接技术
			
1.导入jar包 druid-1.0.9.jar导入数据库驱动jar包 2.定义配置文件 properties形式 可以叫任意名称,可以放置在任意目录下.(意味着不能自动加载,需要手动导入)3.加载配 ...