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

Understanding and application scenarios of enumeration types in TypeScript

Table of contents 1. What is 2. Use Numeric Enume...

Talk about the 8 user instincts behind user experience in design

Editor's note: This article is contributed by...

Solution to transparent font problem after turning on ClearType in IE

The solution to the transparent font problem after...

Docker realizes the connection with the same IP network segment

Recently, I solved the problem of Docker and the ...

Analysis of Context application scenarios in React

Context definition and purpose Context provides a...

HTML cellpadding and cellspacing attributes explained in pictures

Cell -- the content of the table Cell margin (tabl...

Introduction to the use of several special attribute tags in HTML

The following attributes are not very compatible w...

Specific use of GNU Parallel

what is it? GNU Parallel is a shell tool for exec...

Web page creation basic declaration document type description (DTD

Using CSS layout to create web pages that comply w...

How to modify the user and group of a file in Linux

In Linux, when a file is created, the owner of th...

How to remove carriage return characters from text in Linux

When the carriage return character ( Ctrl+M ) mak...

A brief discussion on MySQL large table optimization solution

background The amount of new data in the business...

How to use CSS to write different styles according to sub-elements

The effect we need to achieve: What is needed The...

Linux MySQL root password forgotten solution

When using the MySQL database, if you have not lo...

Detailed explanation of the functions and usage of MySQL common storage engines

This article uses examples to illustrate the func...