Detailed explanation of how to use Vue+element to implement the tag at the top of the page

Detailed explanation of how to use Vue+element to implement the tag at the top of the page

insert image description here

How to write this kind of tag? To summarize the ideas:

1. Page Rendering

Page 1 shows that the array can be stored in the store by looping through the array. (1) Before storing, determine whether there is duplicate data. If there is duplicate data, delete it first and then add it.
(2) Direct push without duplication

 addTag: (state, tag) => {
    const { fullPath, path, meta, query } = tag
    if (tag.path === '/login') {
      return false
    }
    const findIndex = state.tags.findIndex(item => item.path === tag.path)
    console.log(findIndex)
    if (findIndex >= 0) {
      state.tags.splice(findIndex, 1, { fullPath, path, meta, query })
    } else {
      state.tags.push({ fullPath, path, meta, query })
    }
  },

2 When this route adding method is triggered, when the listening route enters, this method is called to carry the route object on the current this instance.

computed: {
currentRoute() {
      return this.$route
    },
},
 watch:
    $route: {
      handler(val) {
        if (val.name) {
          this.addTags()
        }
      },
      // Deep observation monitoring deep: true
    }
  },
  methods:{
  addTags() {
  //this.$store.dispatch is first submitted to action, which asynchronously processes the method in the mutation and changes the tags value in the state this.$store.dispatch('user/addTag', this.currentRoute)
    },}

At this point, there are already values ​​in the tags array. Since the default color is white, it cannot be seen on the page. The next step is to highlight the selected tag.
1element has a parameter that can be set, you can check the documentation.
2 Whether the selected tag value is equal to the page entered by the current route, if it is consistent, it is true.

<span v-for="(tag, index) in tags" :key="index" class="tag-span">
        <el-tag
          :closable="isCloseable"
          :effect="setTagColor(tag)"
          @close="closeTags(tag)"
          @click="toTagRoute(tag)"
        >
          {{ tag.meta.title }}
        </el-tag>
      </span>
 methods:{
 setTagColor(tag) {
      return this.currentRoute.path === tag.path ? 'dark' : 'plain'
    },
    }

At this point, the rendering and selection of the tag is complete.

2. Switch tags back and forth

methods:{
 toTagRoute(tag) {
      this.$router.push({
        path: tag.fullPath || tag.path
      })
    },
}

3. Delete a tag

1 Since it is an array, you cannot determine which one the user deleted, so you need to traverse to find the tag currently selected by the user. Then delete it and update the value in the store.
2. Delete the current tag. Which tag is highlighted? Here is the label before the deleted label, which is the last element of the array.

methods:{
	 closeTags(tag) {
	      console.log(tag, 4444)
	      this.$store.dispatch('user/delTag', tag)
	      this.toLastTagRouter(this.$store.state.user.tags)//Highlight the previous tag to delete
	    },
     toLastTagRouter(tags) {
      //Note that the tags passed in here are deleted, so you cannot use splice==》to change the original array; slice==》does not change the original array and takes the last element of the array const latestView = tags.slice(-1)[0] //The last element of the tags array console.log(latestView)
      if (latestView !== undefined && latestView.path !== undefined) {
        const { fullPath, meta, path, query } = latestView
        this.$router.push({ fullPath, meta, path, query })
      }
    },
}
//action
 delTag({ commit }, tag) {
    commit('delTag', tag)
  },
//mutation
delTag: (state, tag) => {
    //The entries() object becomes a traversable array [0, {name: a, age: '20'}]
    //ForEach and map can also be used here for (const [i, v] of state.tags.entries()) {
      if (v.path === tag.path) {
        state.tags.splice(i, 1)
        break
      }
    }
  },

Delete All Tags

methods:{
 closeAllTags() {
      // Close all tags, leaving only one this.$store.dispatch('user/delAllTags')
      const { fullPath, meta, path, query } = this.$store.state.user.tags[0]
      // Jump to the remaining tag routes this.$router.push({ fullPath, meta, path, query })
    },
}
//action
delAllTags({ commit }) {
    commit('delAllTags')
  },
//mutation
 delAllTags: (state) => {
    state.tags.splice(1, state.tags.length)
  },

This is the end of this article about how to implement the top tag of the page with vue+element. For more relevant vue element top tag content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the interaction between Tags in Vue Elementui and other elements on the page
  • Vue+element uses dynamic loading routing to implement the operation of displaying the three-level menu page
  • vuex+axios+element-ui implements page request loading operation example
  • Implementing the login page based on vue-cli3 and element
  • Vue+element realizes the function of printing page

<<:  The reason why MySQL uses B+ tree as its underlying data structure

>>:  Detailed explanation of the use of HTML header tags

Recommend

Element Timeline implementation

Table of contents Components - Timeline Custom no...

MySQL series: redo log, undo log and binlog detailed explanation

Implementation of transactions The redo log ensur...

Usage and description of HTML tag tbody

The tbody element should be used in conjunction wi...

Example of how to configure the MySQL database timeout setting

Table of contents Preface 1. JDBC timeout setting...

Example of how to implement embedded table with vue+elementUI

During my internship in my senior year, I encount...

Detailed explanation of the practical use of HTML table layout

When is the table used? Nowadays, tables are gene...

How to optimize MySQL index function based on Explain keyword

EXPLAIN shows how MySQL uses indexes to process s...

Learn more about MySQL indexes

1. Indexing principle Indexes are used to quickly...

Two ways to connect WeChat mini program to Tencent Maps

I've been writing a WeChat applet recently an...

Instructions for recovering data after accidental deletion of MySQL database

In daily operation and maintenance work, backup o...

The implementation process of Linux process network traffic statistics

Preface Linux has corresponding open source tools...

Solution to MySQL startup successfully but not listening to the port

Problem Description MySQL is started successfully...