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. 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. <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 forthmethods:{ 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. 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:
|
<<: The reason why MySQL uses B+ tree as its underlying data structure
>>: Detailed explanation of the use of HTML header tags
Table of contents 1. What is 2. Use Numeric Enume...
Editor's note: This article is contributed by...
The solution to the transparent font problem after...
Recently, I solved the problem of Docker and the ...
Context definition and purpose Context provides a...
Cell -- the content of the table Cell margin (tabl...
The following attributes are not very compatible w...
what is it? GNU Parallel is a shell tool for exec...
Using CSS layout to create web pages that comply w...
In Linux, when a file is created, the owner of th...
When the carriage return character ( Ctrl+M ) mak...
background The amount of new data in the business...
The effect we need to achieve: What is needed The...
When using the MySQL database, if you have not lo...
This article uses examples to illustrate the func...