深入了解Vue動(dòng)態(tài)組件和異步組件
1.動(dòng)態(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>
在動(dòng)態(tài)組件上使用keep-alive,可以在組件切換時(shí)保持組件的狀態(tài),避免了重復(fù)渲染的性能問(wèn)題。
2.異步組件
Vue 允許你以一個(gè)工廠函數(shù)的方式定義你的組件,這個(gè)工廠函數(shù)會(huì)異步解析你的組件定義。
Vue.component('async-example', function (resolve, reject) {})
這里可以回顧一下 Vue.js — 組件基礎(chǔ)。
我們使用通過(guò)webpack打包的Vue項(xiàng)目來(lái)介紹異步組件。
<!-- 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>標(biāo)簽里面的內(nèi)容改為:
export default { name: 'App', components: { HelloWorld: () => import('./components/HelloWorld') } }
這樣就實(shí)現(xiàn)了App組件異步加載HelloWorld組件的功能。
我們可以實(shí)現(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ù)也可以返回一個(gè)如下格式的對(duì)象:
const AsyncComponent = () => ({ // 需要加載的組件 (應(yīng)該是一個(gè) `Promise` 對(duì)象) component: import('./MyComponent.vue'), // 異步組件加載時(shí)使用的組件 loading: LoadingComponent, // 加載失敗時(shí)使用的組件 error: ErrorComponent, // 展示加載時(shí)組件的延時(shí)時(shí)間。默認(rèn)值是 200 (毫秒) delay: 200, // 如果提供了超時(shí)時(shí)間且組件加載也超時(shí)了, // 則使用加載失敗時(shí)使用的組件。默認(rèn)值是:`Infinity` timeout: 3000 })
參考:
動(dòng)態(tài)組件 & 異步組件 — Vue.js
以上就是深入了解Vue動(dòng)態(tài)組件和異步組件的詳細(xì)內(nèi)容,更多關(guān)于Vue動(dòng)態(tài)組件和異步組件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解最新vue-cli 2.9.1的webpack存在問(wèn)題
這篇文章主要介紹了最新vue-cli 2.9.1的webpack存在問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12vue+ElementUI 關(guān)閉對(duì)話框清空驗(yàn)證,清除form表單的操作
這篇文章主要介紹了vue+ElementUI 關(guān)閉對(duì)話框清空驗(yàn)證,清除form表單的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08Element-ui中元素滾動(dòng)時(shí)el-option超出元素區(qū)域的問(wèn)題
這篇文章主要介紹了Element-ui中元素滾動(dòng)時(shí)el-option超出元素區(qū)域的問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05Vue3發(fā)送post請(qǐng)求出現(xiàn)400?Bad?Request報(bào)錯(cuò)的解決辦法
這篇文章主要給大家介紹了關(guān)于Vue3發(fā)送post請(qǐng)求出現(xiàn)400?Bad?Request報(bào)錯(cuò)的解決辦法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-02-02laravel5.4+vue+element簡(jiǎn)單搭建的示例代碼
本篇文章主要介紹了laravel5.4+vue+element簡(jiǎn)單搭建的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08vue時(shí)間格式總結(jié)以及轉(zhuǎn)換方法詳解
項(xiàng)目中后臺(tái)返回的時(shí)間有多種形式,時(shí)間戳、ISO標(biāo)準(zhǔn)時(shí)間格式等,我們需要轉(zhuǎn)化展示成能看的懂得時(shí)間格式,下面這篇文章主要給大家介紹了關(guān)于vue時(shí)間格式總結(jié)以及轉(zhuǎn)換方法的相關(guān)資料,需要的朋友可以參考下2022-12-12vue配置nprogress實(shí)現(xiàn)頁(yè)面頂部進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了vue配置nprogress實(shí)現(xiàn)頁(yè)面頂部進(jìn)度條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09