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 Components - Timeline Custom no...
Table of contents 1. Data Source 2. Overall ranki...
Since the introduction of the contentEditable attr...
Implementation of transactions The redo log ensur...
The tbody element should be used in conjunction wi...
Table of contents Preface 1. JDBC timeout setting...
During my internship in my senior year, I encount...
Mac uses Shell (Terminal) SSH to connect to the r...
When is the table used? Nowadays, tables are gene...
EXPLAIN shows how MySQL uses indexes to process s...
1. Indexing principle Indexes are used to quickly...
I've been writing a WeChat applet recently an...
In daily operation and maintenance work, backup o...
Preface Linux has corresponding open source tools...
Problem Description MySQL is started successfully...