vue2.* element tabs tab-pane 動(dòng)態(tài)加載組件操作
一、重要部分
1、 注意 <component :is=item.content></component> :表明模板
<el-tab-pane v-for='(item) in editableTabs' :key='item.name' :label='item.title' :name='item.name' > <component :is=item.content></component> </el-tab-pane>
2、content: ’Jbxx’ ,其中 jbxx 是 模板
addTab (targetName, route) { let newTabName = ++this.tabIndex + ’’ this.editableTabs.push({ title: targetName, name: newTabName, content: ’Jbxx’ }) this.editableTabsValue = newTabName if (targetName === ’基本信息’) { this.show = true } else { this.show = false } // this.$router.push({ // path: route // }) }
二、完整代碼
<template> <el-tabs v-model='editableTabsValue' type='card' closable @tab-remove='removeTab'> <el-tab-pane v-for='(item) in editableTabs' :key='item.name' :label='item.title' :name='item.name' > <component :is=item.content></component> </el-tab-pane> </el-tabs></template> <script>import VueEvent from ’../model/VueEvent.js’import Jbxx from ’./jgxx/Jbxx’ export default { data () { return { show: false, editableTabsValue: ’2’, editableTabs: [{ title: ’Tab 1’, name: ’1’, content: ’’ }, { title: ’Tab 2’, name: ’2’, content: ’’ }], tabIndex: 2 } }, methods: { addTab (targetName, route) { let newTabName = ++this.tabIndex + ’’ this.editableTabs.push({ title: targetName, name: newTabName, content: ’Jbxx’ }) this.editableTabsValue = newTabName if (targetName === ’基本信息’) { this.show = true } else { this.show = false } // this.$router.push({ // path: route // }) }, removeTab (targetName) { let tabs = this.editableTabs let activeName = this.editableTabsValue if (activeName === targetName) { tabs.forEach((tab, index) => { if (tab.name === targetName) { let nextTab = tabs[index + 1] || tabs[index - 1] if (nextTab) { activeName = nextTab.name } } }) } this.editableTabsValue = activeName this.editableTabs = tabs.filter(tab => tab.name !== targetName) } }, mounted () { VueEvent.$on(’to-main’, (name, route) => { this.addTab(name, route) }) }, components: { Jbxx }}</script><style scoped></style>
補(bǔ)充知識(shí):在vue中使用elementUI餓了么框架使用el-tabs,切換Tab如何實(shí)現(xiàn)實(shí)時(shí)加載,以及el-table表格使用總結(jié)...
當(dāng)我們?cè)陂_發(fā)中遇到tab切換,這時(shí)候用el的el-tabs感覺(jué)很方便
但當(dāng)我在把代碼都寫完后,發(fā)現(xiàn)一個(gè)問(wèn)題就是頁(yè)面打開時(shí)
雖然我們只能看見(jiàn)當(dāng)前一個(gè)tab頁(yè),但是vue會(huì)幫你把你寫的所有tab頁(yè)的內(nèi)容都渲染出來(lái)了,只是其他的隱藏了,同時(shí)其他tab的js也都走了一邊,當(dāng)你點(diǎn)擊tab時(shí)js就不會(huì)再去請(qǐng)求后臺(tái)
這種機(jī)制會(huì)造成一個(gè)問(wèn)題,就是如果每個(gè)tab頁(yè)的數(shù)據(jù)都過(guò)大的時(shí)候,可能就會(huì)導(dǎo)致首次打開頁(yè)面卡頓現(xiàn)象,同時(shí)如果數(shù)據(jù)庫(kù)數(shù)據(jù)在實(shí)時(shí)發(fā)生變化的話,比如你一分鐘前打開的這個(gè)頁(yè)面,看的是tab1的內(nèi)容,看了1分鐘后我想看tab2的內(nèi)容,但此時(shí)tab2的內(nèi)容后臺(tái)數(shù)據(jù)庫(kù)已經(jīng)發(fā)生變化了,你能看到的只是1分鐘前的數(shù)據(jù),那該怎么解決這個(gè)問(wèn)題呢?
首先一開始一次加載所有tab的代碼是這樣的↓
<el-tabs v-model='activeName' @tab-click='handleClick' type='border-card'> <el-tab-pane label='待處理' name='first'> <processed/> <!--這里是自定義的子模塊,也就是自定義組件--> </el-tab-pane> <el-tab-pane label='已處理' name='second'> <un-processed/> </el-tab-pane></el-tabs>
這時(shí)候v-if的作用就可以發(fā)揮出來(lái)了,當(dāng)v-if的值為false時(shí)vue是不會(huì)去渲染該標(biāo)簽下的內(nèi)容的
那我們就把tabs下的子模塊標(biāo)簽上加v-if,一開始只設(shè)置其中一個(gè)為true其他都為false,當(dāng)點(diǎn)擊tab切換時(shí)去改變v-if的值,代碼如下↓
<el-tabs v-model='activeName' @tab-click='handleClick' type='border-card'> <el-tab-pane label='待處理' name='first' :key='’first’'> <processed v-if='isFirst'/> </el-tab-pane> <el-tab-pane label='已處理' name='second' :key='’second’'> <un-processed v-if='isSecond'/> </el-tab-pane> </el-tabs>
js的代碼↓
<script>import Breadcrumb from ’@/components/Breadcrumb’import Processed from ’./processed’import UnProcessed from ’./unprocessed’export default { components: { Breadcrumb, Processed, UnProcessed }, data() { return { // 默認(rèn)第一個(gè)Tab activeName: ’first’, isFirst: true, isSecond: false } }, methods: { handleClick(tab) { if (tab.name === ’first’) { this.isFirst = true this.isSecond = false } else if (tab.name === ’second’) { this.isFirst = false this.isSecond = true } } }}</script>
這樣就可以完美解決上面的問(wèn)題,首次加載頁(yè)面只會(huì)渲染其中一個(gè)tab的內(nèi)容,同時(shí)點(diǎn)擊tab切換時(shí)頁(yè)面重新渲染頁(yè)面,good!
el-table中動(dòng)態(tài)表頭的寫法
其實(shí)就是一個(gè)v-for循環(huán),根據(jù)后臺(tái)返回?cái)?shù)據(jù)生成對(duì)應(yīng)表頭
<el-table-column v-for='item in tableHeader' :key='item.key' :prop='item.key' :label='item.name' :formatter='item.formatter' show-overflow-tooltip></el-table-column>
js里的數(shù)據(jù)綁定:
tableHeader: [ { name: ’手機(jī)號(hào)碼’, key: ’partnerPhone’ }, { name: ’姓名’, key: ’partnerName’ }, { name: ’職位’, key: ’position’, formatter: this.posFormatter }, { name: ’團(tuán)隊(duì)’, key: ’teamName’ }, { name: ’代理商’, key: ’agentName’ }, { name: ’狀態(tài)’, key: ’state’, formatter: this.stFormatter } ]
以上這篇vue2.* element tabs tab-pane 動(dòng)態(tài)加載組件操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法2. idea設(shè)置提示不區(qū)分大小寫的方法3. HTTP協(xié)議常用的請(qǐng)求頭和響應(yīng)頭響應(yīng)詳解說(shuō)明(學(xué)習(xí))4. IntelliJ IDEA創(chuàng)建web項(xiàng)目的方法5. VMware中如何安裝Ubuntu6. ASP.NET MVC通過(guò)勾選checkbox更改select的內(nèi)容7. .NET SkiaSharp 生成二維碼驗(yàn)證碼及指定區(qū)域截取方法實(shí)現(xiàn)8. CentOS郵件服務(wù)器搭建系列—— POP / IMAP 服務(wù)器的構(gòu)建( Dovecot )9. docker容器調(diào)用yum報(bào)錯(cuò)的解決辦法10. django創(chuàng)建css文件夾的具體方法
