實現(xiàn)動態(tài)導(dǎo)航

動態(tài)導(dǎo)航顧名思義,就是通過點擊menu菜單,實現(xiàn)上方動態(tài)路由的現(xiàn)實,方便用戶使用。那如何封裝一個動態(tài)組件:

<template>
  <div class="navi">  //判斷是否是當(dāng)前路由
    <div :class="currentPath == v.url ? 'navi_div active' : 'navi_div'" v-for="(v, i) in naviArrs" :key="i" @mousemove="moveActive(v)" @click="toPath(v)">
      <span>{{ v.name }}</span>
      <i class="el-icon-circle-close" @click="delNavi(v)"></i>
    </div>
  </div>
</template>

<script>
import typeUtils from "commons/common/typeUtils";
import { mapActions,mapState } from "vuex";

export default {
  data() {
    return {
      naviArrs: [],
      currentPath:this.$route.path //當(dāng)前路由
    };
  },
  props: {
    naviArr: {
      type: Array,
      default: () => {
        return [];
      },
    }
  },
  created() {  //進行去重,結(jié)合上一篇文章的menu
    this.naviArrs = typeUtils.arrSet(this.naviArr, "name");
  },
  watch: {
    naviArr(val, oldVal) {
      this.naviArrs = typeUtils.arrSet(val, "name");
      this.pathListFunAsync(this.naviArrs)
    },
    $route(val,oldVal){    //***監(jiān)聽路由的變化
        this.currentPath = val.path
    },
  },
  methods:{
    ...mapActions('paths',['pathListFunAsync']),
    moveActive(val){
        this.currentPath = val.url
    },
    delNavi(val){
        this.naviArrs.forEach((v,i) => {
            if (v.name == val.name) {
                this.naviArrs.splice(i,1)
            }
        })
    },
    toPath(val){
        this.$router.push(val.url)
    }
  }
};
</script>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容