Newer
Older
smartKitchenFront / src / components / mycomponent / navItem.vue
liuyangyingjie on 26 Oct 2022 1 KB first commit
<template>
  <div class="nav_container">
    <div class="iconBox" @click.prevent="change()">
      <i class="el-icon-s-unfold" v-if="!isCollapse"></i>
      <i class="el-icon-s-fold" v-else></i>
      <!-- <menu-unfold-outlined v-if="true" />
      <menu-fold-outlined  v-else/> -->
    </div>
    <div>
      <span v-for="(item, index) in list" :key="index" style="margin: 0 5px">
        <!-- <el-button size="small" @click="changeUrl(item.url)">{{ item.title }}</el-button> -->
        <el-tag  :key="index" closable    @click="changeUrl(item.url)"  @close="handleClose(index)">
          {{ item.title }}
        </el-tag>
      </span>
    </div>
  </div>
</template>

<script>
export default {
  model: {
    prop: "isCollapse",
    event: "changeIsCollapse",
  },
  props: ["isCollapse"],
  data() {
    return {
      list: [],
    };
  },
  watch: {
    $route: {
      handler: function (val, oldVal) {
        //只判断后一条
        //防止第一条监听不到
        const index = this.list.findIndex((item) => item.url === val.path);
        if (index != -1 ||this.list.length>10) {
          this.list.splice(index, 1); //存在则删除
        }
        this.list.push({ url: val.fullPath, title: val.meta.title });
      },
      // 强制立即执行回调
      immediate: true,
      // 深度观察监听
      deep: true,
    },
  },
  methods: {
    handleClose(index){
      this.list.splice(index, 1)
    },
    changeUrl(url) {
      this.$router.push(url);
    },
    change() {
      console.log(!this.isCollapse);
      this.$emit("changeIsCollapse", !this.isCollapse);
    },
  },
};
</script>

<style lang="scss" scoped>
.nav_container {
  display: flex;
  padding: 0 10px;
  box-sizing: border-box;
  align-items: center;
  height: 40px;
  box-shadow: 0px 0px 20px 3px #999;
  position: relative;
  z-index: 1;
  .iconBox {
    // background: red;
    margin: 0 15px 0 0;
    height: 100%;
    width: 40px;
    text-align: center;
    line-height: 40px;
  }
}
</style>