| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 | <template>  <div class="container">    <el-tag      class="filter-search"      v-for="item in activeItems"      :key="item.key"      @click="toggleItem(item)"      :effect="isActive(item) ? 'dark' : 'plain'">      {{desced(item)}}    </el-tag>    <!-- <div      class="filter-search"      :class="{active: isActive(item)}" v-for="item in activeItems" :key="item.key"      @click="toggleItem(item)"    >      <span>{{desced(item)}}</span>    </div> -->  </div></template><script>import { errorType } from '@/views/main/constant'import cleanDeep from 'clean-deep'const typesByUrl = {}for (const key in errorType) {  if (Object.hasOwnProperty.call(errorType, key)) {    const item = errorType[key];    if (!typesByUrl[item.url]) {      typesByUrl[item.url] = []    }    typesByUrl[item.url].push({      ...item,      key    })  }}// console.log(typesByUrl)export default {  name: 'filter-search',  props: {    desc: {      type: String,      default: '已筛选部分数据'    },    searchKey: {      type: String,      default: 'search'    },    keys: {      type: Array,      default: () => []    },    moreKeys: {      type: Array,      default: () => []    },  },  data() {    return{      initSearch: {}    }  },  computed: {    hasSearch() {      return this.$route.query[this.searchKey]    },    hasForm() {      const keys = {}      for (const item of this.keys) {        keys[item] = this.$route.query[item]      }      return !!Object.keys(cleanDeep(keys)).length    },    show() {      return this.hasSearch || this.hasForm    },    activeItems() {      return typesByUrl[this.$route.path]    }  },  mounted() {    this.initSearch = this.$route.query    // console.log(this.activeItems)  },  watch: {    $route() {      this.$emit('setTimeForSearch')      this.$emit('reload', this.show)    },  },  methods: {    desced(item) {      return item.name ? `仅显示: ${item.name}` : this.desc    },    isActive(item) {      return item.key === this.$route.query.filter_type    },    toggleItem(item) {      if (this.isActive(item)) {        this.close()      } else {        this.$router.replace({          path: item.url,          query: {            ...this.$route.query,            ...item.query,            filter_type: item.key,            [this.searchKey]: this.initSearch[this.searchKey]          }        })        this.$emit('setTimeForSearch')        // this.$emit('reload', true)      }    },    close() {      const keys = {}      for (const item of [...this.keys, ...this.moreKeys]) {        keys[item] = undefined      }      this.$router.replace({        query: {          ...this.$route.query,          [this.searchKey]: undefined,          filter_type: undefined,          ...keys        }      })      // this.$emit('reload')    }  }}</script><style lang="less" scoped>.container{  display: flex;}  .filter-search{    margin-left: 10px;    display: flex;    align-items: center;    cursor: pointer;    &.active{      color: #e6a23c;    }    >img{      width: 20px;      height: auto;    }    >span{      margin: 0 6px    }    >i{      font-size: 16px;    }  }</style>
 |