最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

深入了解Vue動態(tài)組件和異步組件

 更新時間:2021年01月26日 12:03:07   作者:gzhjj  
這篇文章主要介紹了深入了解Vue動態(tài)組件和異步組件的相關資料,幫助大家更好的理解和學習vue組件的相關知識,感興趣的朋友可以了解下

1.動態(tài)組件

<!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>

在動態(tài)組件上使用keep-alive,可以在組件切換時保持組件的狀態(tài),避免了重復渲染的性能問題。

2.異步組件

Vue 允許你以一個工廠函數(shù)的方式定義你的組件,這個工廠函數(shù)會異步解析你的組件定義。

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')
 }
}

這樣就實現(xiàn)了App組件異步加載HelloWorld組件的功能。

我們可以實現(xiàn)按需加載。

<!-- 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>

這里的異步組件工廠函數(shù)也可以返回一個如下格式的對象:

const AsyncComponent = () => ({
 // 需要加載的組件 (應該是一個 `Promise` 對象)
 component: import('./MyComponent.vue'),
 // 異步組件加載時使用的組件
 loading: LoadingComponent,
 // 加載失敗時使用的組件
 error: ErrorComponent,
 // 展示加載時組件的延時時間。默認值是 200 (毫秒)
 delay: 200,
 // 如果提供了超時時間且組件加載也超時了,
 // 則使用加載失敗時使用的組件。默認值是:`Infinity`
 timeout: 3000
})

參考:

動態(tài)組件 & 異步組件 — Vue.js

以上就是深入了解Vue動態(tài)組件和異步組件的詳細內容,更多關于Vue動態(tài)組件和異步組件的資料請關注腳本之家其它相關文章!

相關文章

  • Vue基礎學習之項目整合及優(yōu)化

    Vue基礎學習之項目整合及優(yōu)化

    這篇文章主要給大家介紹了關于Vue基礎學習之項目整合及優(yōu)化的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Vue具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-06-06
  • Vue仿手機qq的實例代碼(demo)

    Vue仿手機qq的實例代碼(demo)

    Vue.js(讀音 /vju&#720;/, 類似于 view) 是一套構建用戶界面的漸進式框架。這篇文章給大家介紹Vue仿手機qq的實例代碼,需要的的朋友參考下吧
    2017-09-09
  • Vue3內置組件Teleport使用方法詳解

    Vue3內置組件Teleport使用方法詳解

    這篇文章主要介紹了Vue3內置組件Teleport使用方法,Teleport是Vue 3.0 新增的一個內置組件,主要是為了解決一些特殊場景下模態(tài)對話框組件、組件的渲染,帶著些許的了解一起走進下面文章的詳細內容吧
    2021-10-10
  • vue3利用keepAlive緩存頁面實例詳解

    vue3利用keepAlive緩存頁面實例詳解

    <keep-alive> 是一個抽象組件,它自身不會渲染一個DOM元素,也不會出現(xiàn)在組件的父組件鏈中,下面這篇文章主要給大家介紹了關于vue3利用keepAlive緩存頁面的相關資料,需要的朋友可以參考下
    2022-11-11
  • Vue中動態(tài)路由加載與ESLint錯誤排查全指南

    Vue中動態(tài)路由加載與ESLint錯誤排查全指南

    在現(xiàn)代前端開發(fā)中,Vue.js?結合?Webpack?的動態(tài)路由加載能顯著提升應用性能,本文將通過一個實際案例,詳細分析動態(tài)路由加載的常見錯誤,希望對大家有所幫助
    2025-04-04
  • vue開發(fā)chrome插件,實現(xiàn)獲取界面數(shù)據(jù)和保存到數(shù)據(jù)庫功能

    vue開發(fā)chrome插件,實現(xiàn)獲取界面數(shù)據(jù)和保存到數(shù)據(jù)庫功能

    這篇文章主要介紹了vue開發(fā)chrome插件,實現(xiàn)獲取界面數(shù)據(jù)和保存到數(shù)據(jù)庫功能的示例,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下
    2020-12-12
  • vue使用Google Recaptcha驗證的實現(xiàn)示例

    vue使用Google Recaptcha驗證的實現(xiàn)示例

    我們最近的項目中需要使用谷歌機器人驗證,所以就動手實現(xiàn)一下,本文就來詳細的介紹一下vue Google Recaptcha驗證,感興趣的可以了解一下
    2021-08-08
  • 關于vant的時間選擇器使用方式

    關于vant的時間選擇器使用方式

    這篇文章主要介紹了關于vant的時間選擇器使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Vue滾動頁面到指定位置的實現(xiàn)及避坑

    Vue滾動頁面到指定位置的實現(xiàn)及避坑

    這篇文章主要介紹了Vue滾動頁面到指定位置的實現(xiàn)及避坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • vue.extend,mixins和vue.component的區(qū)別及說明

    vue.extend,mixins和vue.component的區(qū)別及說明

    Vue.extend 創(chuàng)建Vue的子類,可視為組件構造函數(shù),Vue.mixin 允許全局添加方法或屬性,方便所有組件使用,Vue.component 是插件注冊方法,通過Vue.extend創(chuàng)建的組件實例可以注冊到Vue全局,使其在任何組件中可用
    2024-09-09

最新評論

通江县| 高碑店市| 郯城县| 浦城县| 竹山县| 新余市| 太湖县| 邮箱| 东阳市| 霍州市| 泽普县| 邛崃市| 龙州县| 曲阳县| 德安县| 无锡市| 禹州市| 通江县| 海口市| 庆云县| 商河县| 马龙县| 扎兰屯市| 海丰县| 古浪县| 望江县| 丰都县| 大关县| 彭泽县| 西和县| 长宁县| 上饶市| 黔西| 孝昌县| 永德县| 怀远县| 阿图什市| 铅山县| 乡宁县| 游戏| 崇义县|