电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:5日期:2022-10-08 15:44:06
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' v-bind: @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動態(tài)組件和異步組件

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

2.異步組件

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

Vue.component(’async-example’, function (resolve, reject) {})

這里可以回顧一下 Vue.js — 組件基礎(chǔ)。

我們使用通過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>標(biāo)簽里面的內(nèi)容改為:

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 = () => ({ // 需要加載的組件 (應(yīng)該是一個 `Promise` 對象) component: import(’./MyComponent.vue’), // 異步組件加載時使用的組件 loading: LoadingComponent, // 加載失敗時使用的組件 error: ErrorComponent, // 展示加載時組件的延時時間。默認(rèn)值是 200 (毫秒) delay: 200, // 如果提供了超時時間且組件加載也超時了, // 則使用加載失敗時使用的組件。默認(rèn)值是:`Infinity` timeout: 3000})參考:

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

以上就是深入了解Vue動態(tài)組件和異步組件的詳細(xì)內(nèi)容,更多關(guān)于Vue動態(tài)組件和異步組件的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 东莞注册公司-代办营业执照-东莞公司注册代理记账-极刻财税 | 转子泵_凸轮泵_凸轮转子泵厂家-青岛罗德通用机械设备有限公司 | 上海恒驭仪器有限公司-实验室平板硫化机-小型平板硫化机-全自动平板硫化机 | 磷酸肌酸二钠盐,肌酐磷酰氯-沾化欣瑞康生物科技 | 高低温万能试验机-复合材料万能试验机-馥勒仪器 | 水上浮桥-游艇码头-浮动码头-游船码头-码瑞纳游艇码头工程 | 真空包装机-诸城市坤泰食品机械有限公司 | 建筑资质代办-建筑企业资质代办机构-建筑资质代办公司 | 活性氧化铝|无烟煤滤料|活性氧化铝厂家|锰砂滤料厂家-河南新泰净水材料有限公司 | 电梯乘运质量测试仪_电梯安全评估测试仪-武汉懿之刻 | 注塑机-压铸机-塑料注塑机-卧式注塑机-高速注塑机-单缸注塑机厂家-广东联升精密智能装备科技有限公司 | 小型玉石雕刻机_家用玉雕机_小型万能雕刻机_凡刻雕刻机官网 | 防爆电机_防爆电机型号_河南省南洋防爆电机有限公司 | 压片机_高速_单冲_双层_花篮式_多功能旋转压片机-上海天九压片机厂家 | 隔离变压器-伺服变压器--输入输出电抗器-深圳市德而沃电气有限公司 | 石家庄小程序开发_小程序开发公司_APP开发_网站制作-石家庄乘航网络科技有限公司 | TPE_TPE热塑性弹性体_TPE原料价格_TPE材料厂家-惠州市中塑王塑胶制品公司- 中塑王塑胶制品有限公司 | 杭州画室_十大画室_白墙画室_杭州美术培训_国美附中培训_附中考前培训_升学率高的画室_美术中考集训美术高考集训基地 | PE拉伸缠绕膜,拉伸缠绕膜厂家,纳米缠绕膜-山东凯祥包装 | 圆周直径尺-小孔内视镜-纤维研磨刷-东莞市高腾达精密工具 | 电子天平-华志电子天平厂家| 打造全球沸石生态圈 - 国投盛世 锂电混合机-新能源混合机-正极材料混料机-高镍,三元材料混料机-负极,包覆混合机-贝尔专业混合混料搅拌机械系统设备厂家 | 全自动面膜机_面膜折叠机价格_面膜灌装机定制_高速折棉机厂家-深圳市益豪科技有限公司 | 华东师范大学在职研究生招生网_在职研究生招生联展网 | 99文库_实习生实用的范文资料文库站 | 油缸定制-液压油缸厂家-无锡大鸿液压气动成套有限公司 | 定制/定做衬衫厂家/公司-衬衫订做/订制价格/费用-北京圣达信 | 扬尘在线监测系统_工地噪声扬尘检测仪_扬尘监测系统_贝塔射线扬尘监测设备「风途物联网科技」 | 民用音响-拉杆音响-家用音响-ktv专用音响-万昌科技 | 手术室净化厂家_成都实验室装修公司_无尘车间施工单位_洁净室工程建设团队-四川华锐16年行业经验 | 低温柔性试验仪-土工布淤堵-沥青车辙试验仪-莱博特(天津)试验机有限公司 | 退火炉,燃气退火炉,燃气热处理炉生产厂家-丹阳市丰泰工业炉有限公司 | 闪蒸干燥机-喷雾干燥机-带式干燥机-桨叶干燥机-[常州佳一干燥设备] | 展厅设计-展馆设计-专业企业展厅展馆设计公司-昆明华文创意 | 【官网】博莱特空压机,永磁变频空压机,螺杆空压机-欧能优 | 合肥通道闸-安徽车牌识别-人脸识别系统厂家-安徽熵控智能技术有限公司 | 仿真茅草_人造茅草瓦价格_仿真茅草厂家_仿真茅草供应-深圳市科佰工贸有限公司 | 压砖机_电动螺旋压力机_粉末成型压力机_郑州华隆机械tel_0371-60121717 | 北京中航时代-耐电压击穿试验仪厂家-电压击穿试验机 | 跨境物流_美国卡派_中大件运输_尾程派送_海外仓一件代发 - 广州环至美供应链平台 | 天津蒸汽/热水锅炉-电锅炉安装维修直销厂家-天津鑫淼暖通设备有限公司 |