Vue折疊面板組件的封裝
本文實(shí)例為大家分享了Vue折疊面板組件的封裝代碼,供大家參考,具體內(nèi)容如下
該組件使用了 Element 的一些 icon 圖標(biāo),以及 過(guò)渡動(dòng)畫(huà) el-collapse-transition,需安裝 element

具體使用方法,主要知識(shí)點(diǎn) provide ,inject,this.$children 和 _uid (vue中無(wú)論遞歸組件,還是自定義組件,每個(gè)組件都有唯一的_uid)
<!-- 折疊組件 --> <navigation-bar v-model="barName" accordion> ? <navigation-bar-item label="測(cè)試1" name="1" line>測(cè)試1</navigation-bar-item> ? <navigation-bar-item label="測(cè)試2" name="2" line>測(cè)試2</navigation-bar-item> ? <navigation-bar-item label="測(cè)試3" name="3">測(cè)試3</navigation-bar-item> </navigation-bar>
navigation-bar 組件代碼如下
<template>
? <div>
? ? <slot></slot>
? </div>
</template>
<script>
export default {
? name: "navigationBar",
? provide () {
? ? return {
? ? ? Bar: this
? ? }
? },
? props: {
? ? value: {
? ? ? type: String,
? ? ? default: ''
? ? },
? ? accordion: {
? ? ? type: Boolean,
? ? ? default: false
? ? }
? },
? methods: {
? ? changeState (id) {
? ? ? this.$children.forEach(item => {
? ? ? ? if (item._uid !== id) {
? ? ? ? ? item.isShow = false
? ? ? ? } else {
? ? ? ? ? item.isShow = !item.isShow
? ? ? ? }
? ? ? })
? ? }
? },
}
</script>
<style scoped>
</style>navigation-bar-item 組件代碼如下,el-image 的圖片地址使用的本地圖片,請(qǐng)更換自己的路徑
<template>
? <div :class="line && !isShow ? 'content' : ''">
? ? <div class="navigationBar" @click="foldClick">
? ? ? <div class="navigationBarLeft"></div>
? ? ? <div class="navigationBarRight">
? ? ? ? <span>{{label}}</span>
? ? ? ? <div class="navigationBarIcon">
? ? ? ? ? <el-image style="width: 18px; height: 18px" :src="require('../assets/img/doubt.png')" ></el-image>
? ? ? ? ? <i :class="isShow ? 'rotate' : 'rotate1'" ref="foldIcon" style="margin-left: 10px" class="el-icon-arrow-down"></i>
? ? ? ? </div>
? ? ? </div>
? ? </div>
? ? <el-collapse-transition>
? ? ? <div v-show="isShow">
? ? ? ? <slot></slot>
? ? ? </div>
? ? </el-collapse-transition>
? </div>
</template>
<script>
export default {
? name: "navigationBarItem",
? inject: ['Bar'],
? props:{
? ? label:{
? ? ? type: String,
? ? ? required: true
? ? },
? ? name:{
? ? ? type: String,
? ? ? default: ''
? ? },
? ? line: {
? ? ? type: Boolean,
? ? ? default: false
? ? }
? },
? data() {
? ? return {
? ? ? isShow: false
? ? }
? },
? mounted() {
? ? // 默認(rèn)展開(kāi)
? ? this.Bar.value === this.name ? this.isShow = true : ''
? },
? methods: {
? ? // 導(dǎo)航條折疊
? ? foldClick() {
? ? ? if (this.Bar.accordion) {
? ? ? ? this.Bar.changeState(this._uid)
? ? ? } else {
? ? ? ? this.isShow = !this.isShow;
? ? ? }
? ? }
? }
}
</script>
<style scoped>
? .navigationBar {
? ? display: flex;
? }
? .navigationBar:hover {
? ? cursor: pointer;
? }
? .navigationBarLeft {
? ? width: 6px;
? ? height: 25px;
? ? background-color: #3179F4;
? }
? .navigationBarRight {
? ? flex: 1;
? ? display: flex;
? ? height: 25px;
? ? background-color: #F2F2F2;
? ? justify-content: space-between;
? ? padding: 0 10px;
? ? align-items: center;
? ? font-size: 14px;
? }
? .navigationBarIcon {
? ? display: flex;
? ? align-items: center;
? }
? .rotate {
? ? transform: rotate(180deg);
? ? transition: transform .3s;
? }
? .rotate1 {
? ? transform: rotate(0deg);
? ? transition: transform .3s;
? }
? .content {
? ? border-bottom: 1px solid #DCDFE6;
? }
</style>以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
100行代碼實(shí)現(xiàn)一個(gè)vue分頁(yè)組功能
今天用vue來(lái)實(shí)現(xiàn)一個(gè)分頁(yè)組件,總體來(lái)說(shuō),vue實(shí)現(xiàn)比較簡(jiǎn)單,樣式部分模仿了elementUI。接下來(lái)本文通過(guò)實(shí)例代碼給大家介紹100行代碼實(shí)現(xiàn)一個(gè)vue分頁(yè)組功能,感興趣的朋友跟隨小編一起看看吧2018-11-11
vue3封裝簡(jiǎn)易的vue-echarts問(wèn)題
這篇文章主要介紹了vue3封裝簡(jiǎn)易的vue-echarts問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
通過(guò)vue-router懶加載解決首次加載時(shí)資源過(guò)多導(dǎo)致的速度緩慢問(wèn)題
這篇文章主要介紹了vue-router懶加載解決首次加載時(shí)資源過(guò)多導(dǎo)致的速度緩慢問(wèn)題,文中單獨(dú)給大家介紹了vue router路由懶加載問(wèn)題,需要的朋友可以參考下2018-04-04
nginx+vue.js實(shí)現(xiàn)前后端分離的示例代碼
這篇文章主要介紹了nginx+vue.js實(shí)現(xiàn)前后端分離的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
antdv的table因數(shù)據(jù)量過(guò)大導(dǎo)致的卡頓問(wèn)題及解決
這篇文章主要介紹了antdv的table因數(shù)據(jù)量過(guò)大導(dǎo)致的卡頓問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
vue-cli 3如何使用vue-bootstrap-datetimepicker日期插件
這篇文章主要介紹了vue-cli 3如何使用vue-bootstrap-datetimepicker日期插件,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下2021-02-02

