本篇文章給大家分享的是有關如何在Vue.js中使用標簽頁組件,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
index.html
標簽頁組件 標簽一的內容 標簽二的內容 標簽三的內容
樣式表 style.css
[v-clock]{ display: none; } .tabs{ font-size: 14px; color: #657180 } .tabs-bar:after{ content: ''; display: block; width: 100%; height: 1px; background: #d7dde4; margin-top: -1px; } .tabs-tab{ display: inline-block; padding: 4px 16px; margin-right: 6px; background: #fff; border: 1px solid #d7dde4; cursor: pointer; position: relative; } .tabs-tab-active{ color: #ee99ff; border-top: 1px solid #3399ff; border-bottom: 1px solid #fff; } .tabs-tab-active:befor{ content: ''; display: block; height: 1px; background: #3399ff; position: absolute; top: 0; left: 0; right: 0; } .tabs-content{ padding: 8px 0; }
標簽頁外層的組件tabs tabs.js
Vue.component('tabs',{ template: '\\', props: { value: { type: [String, Number] } }, data: function () { return { //用于渲染tabs的標題 currentValue: this.value, navList: [] } }, methods: { tabCls(item){ return [ 'tabs-tab', { 'tabs-tab-active': item.name === this.currentValue } ] }, getTabs(){ //通過遍歷子組件,得到所有的pane組件 return this.$children.filter(function (item) { return item.$options.name === 'pane'; }); }, updateNav(){ this.navList = []; //設置對this的引用,在function回調里,this的指向的并不是Vue實例 var _this = this; this.getTabs().forEach((pane, index) => { _this.navList.push({ label: pane.label, name: pane.name || index }); //如果沒有給pane設置name,默認設置它的索引 if(!pane.name) pane.name = index; //設置當前選中的tab的索引 if(index === 0){ if(!_this.currentValue){ _this.currentValue = pane.name || index; } } }); this.updateStatus(); }, updateStatus(){ var tabs = this.getTabs(); var _this = this; //顯示當前選中的tab對應的pane組件,隱藏沒有選中的 tabs.forEach(tab => { return tab.show = tab.name === _this.currentValue; }); }, handleChange: function (index) { var nav = this.navList[index]; var name = nav.name; this.currentValue = name; this.$emit('input', name); this.$emit('on-click', name); } }, watch: { value: val => { this.currentValue = val; }, currentValue: function () { this.updateStatus(); } } });\ \\\ {{item.label}}\\\ \\\
標簽頁嵌套的組件pane pane.js
Vue.component('pane',{ name: 'pane', template: '\\', data: function () { return { show: true } }, props: { name: String }, label: { type: String, default: '' }, methods: { updateNav: function () { this.$parent.updateNav(); } }, watch: { label: function () { this.updateNav(); } }, mounted: function () { this.updateNav(); } });\
以上就是如何在Vue.js中使用標簽頁組件,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注創新互聯成都網站設計公司行業資訊頻道。
另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。