在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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> 在动态组件上使用keep-alive,可以在组件切换时保持组件的状态,避免了重复渲染的性能问题。 2.异步组件
|
请发表评论