关于tag标签组件


  在项目中遇到这么一个需求,点击侧边栏时会弹出标签,点击页面的表格时也会弹出标签,在网上找的资料只有在侧边栏点击的时候弹标签,没有表格内的内容点击时弹标签,所以自己将tag组件改了一下,还好,完美适合需求

 在网上百度的tagView多页签的代码

 // tagview组件下的index.vue






// tagview组件下的ScrollPane.vue




// vuex下的tagsView.js
const state = {
  visitedViews: [],
  cachedViews: []
}

const mutations = {
  ADD_VISITED_VIEW: (state, view) => {
    if (state.visitedViews.some(v => v.path === view.path)) return
    state.visitedViews.push(
      Object.assign({}, view, {
        title: view.meta.title || 'no-name'
      })
    )
  },
  ADD_CACHED_VIEW: (state, view) => {
    if (state.cachedViews.includes(view.name)) return
    if (!view.meta.noCache) {
      state.cachedViews.push(view.name)
    }
  },

  DEL_VISITED_VIEW: (state, view) => {
    for (const [i, v] of state.visitedViews.entries()) {
      if (v.path === view.path) {
        state.visitedViews.splice(i, 1)
        break
      }
    }
  },
  DEL_CACHED_VIEW: (state, view) => {
    const index = state.cachedViews.indexOf(view.name)
    index > -1 && state.cachedViews.splice(index, 1)
  },

  DEL_OTHERS_VISITED_VIEWS: (state, view) => {
    state.visitedViews = state.visitedViews.filter(v => {
      return v.meta.affix || v.path === view.path
    })
  },
  DEL_OTHERS_CACHED_VIEWS: (state, view) => {
    const index = state.cachedViews.indexOf(view.name)
    if (index > -1) {
      state.cachedViews = state.cachedViews.slice(index, index + 1)
    } else {
      // if index = -1, there is no cached tags
      state.cachedViews = []
    }
  },

  DEL_ALL_VISITED_VIEWS: state => {
    // keep affix tags
    const affixTags = state.visitedViews.filter(tag => tag.meta.affix)
    state.visitedViews = affixTags
  },
  DEL_ALL_CACHED_VIEWS: state => {
    state.cachedViews = []
  },

  UPDATE_VISITED_VIEW: (state, view) => {
    for (let v of state.visitedViews) {
      if (v.path === view.path) {
        v = Object.assign(v, view)
        break
      }
    }
  }
}

const actions = {
  addView({ dispatch }, view) {
    dispatch('addVisitedView', view)
    dispatch('addCachedView', view)
  },
  addVisitedView({ commit }, view) {
    commit('ADD_VISITED_VIEW', view)
  },
  addCachedView({ commit }, view) {
    commit('ADD_CACHED_VIEW', view)
  },

  delView({ dispatch, state }, view) {
    return new Promise(resolve => {
      dispatch('delVisitedView', view)
      dispatch('delCachedView', view)
      resolve({
        visitedViews: [...state.visitedViews],
        cachedViews: [...state.cachedViews]
      })
    })
  },
  delVisitedView({ commit, state }, view) {
    return new Promise(resolve => {
      commit('DEL_VISITED_VIEW', view)
      resolve([...state.visitedViews])
    })
  },
  delCachedView({ commit, state }, view) {
    return new Promise(resolve => {
      commit('DEL_CACHED_VIEW', view)
      resolve([...state.cachedViews])
    })
  },

  delOthersViews({ dispatch, state }, view) {
    return new Promise(resolve => {
      dispatch('delOthersVisitedViews', view)
      dispatch('delOthersCachedViews', view)
      resolve({
        visitedViews: [...state.visitedViews],
        cachedViews: [...state.cachedViews]
      })
    })
  },
  delOthersVisitedViews({ commit, state }, view) {
    return new Promise(resolve => {
      commit('DEL_OTHERS_VISITED_VIEWS', view)
      resolve([...state.visitedViews])
    })
  },
  delOthersCachedViews({ commit, state }, view) {
    return new Promise(resolve => {
      commit('DEL_OTHERS_CACHED_VIEWS', view)
      resolve([...state.cachedViews])
    })
  },

  delAllViews({ dispatch, state }, view) {
    return new Promise(resolve => {
      dispatch('delAllVisitedViews', view)
      dispatch('delAllCachedViews', view)
      resolve({
        visitedViews: [...state.visitedViews],
        cachedViews: [...state.cachedViews]
      })
    })
  },
  delAllVisitedViews({ commit, state }) {
    return new Promise(resolve => {
      commit('DEL_ALL_VISITED_VIEWS')
      resolve([...state.visitedViews])
    })
  },
  delAllCachedViews({ commit, state }) {
    return new Promise(resolve => {
      commit('DEL_ALL_CACHED_VIEWS')
      resolve([...state.cachedViews])
    })
  },

  updateVisitedView({ commit }, view) {
    commit('UPDATE_VISITED_VIEW', view)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

1第一步,将组件TagsView目录放置到src/components , 并全局注册

import TagsView from './TagsView'
Vue.component('TagsView', TagsView)

第二步,将Vuex模块tagsView.js放置到 src/store/modules

并在store中引入该模块

import tagsView from './modules/tagsView'
const store = new Vuex.Store({
  modules: {
    app,
    settings,
    user,
    permission,
    tagsView
  },
  getters
})

第三步,在src/layout/Index.vue中引入该组件

将以上代码复制到自己的项目中然后根据自己的需要相对应的更改(以上代码只能实现侧边栏点击增加tag标签)

我的项目需求中因为牵扯到侧边栏点击添加tag标签和侧边栏点击出来的表格中点击表格项也要添加相应的标签,所以对上面的代码进行修改

 主要的思路是根据fullPath进行判断,因为每个页面的path可能一样,但是fullPath一定不同

// 更改后的TagsView组件下的index.vue
const state = {
  visitedViews: [],
  cachedViews: [],
  delRoute: []
}

const mutations = {
  ADD_VISITED_VIEW: (state, view) => {
    // debugger
    if (view.query.id) {
      // debugger
      if (state.visitedViews.some(v => v.fullPath === view.fullPath)) {
        // debugger
        return
      }
      // debugger
      state.visitedViews.push(
        Object.assign({}, view, {
          title: view.meta.title || view.meta.tagtitle || 'no-name'
        })
      )
    } else if (view.path === '/') {
      state.visitedViews = []
    } else {
      if (view.query.enableRestart !== undefined) {
        if (state.visitedViews.some(v => v.fullPath === view.fullPath)) {
          return false
        } else {
          state.visitedViews.push(
            Object.assign({}, view, {
              title: view.meta.tagtitle || view.meta.title || 'no-name'
            })
          )
        }
      } else {
        if (state.visitedViews.some(v => v.fullPath === view.fullPath)) {
          return false
        } else {
          state.visitedViews.push(
            Object.assign({}, view, {
              title: view.meta.title || view.meta.tagtitle || 'no-name'
            })
          )
        }
      }
    }
  },
  ADD_CACHED_VIEW: (state, view) => {
    if (state.cachedViews.includes(view.name)) return
    if (state.cachedViews.includes(view.meta.tagtitle)) return
    if (!view.meta.noCache) {
      state.cachedViews.push(view.name || view.meta.tagtitle)
    }
  },
  // ADD_CACHED_VIEW: (state, view) => {
  //   if (!state.cachedViews.includes(view.fullPath)) {
  //     state.cachedViews.push(view.fullPath)
  //   }
  // },

  DEL_VISITED_VIEW: (state, view) => {
    for (const [i, v] of state.visitedViews.entries()) {
      if (v.fullPath === view.fullPath) {
        state.visitedViews.splice(i, 1)
        state.delRoute.push(view.fullPath)
        break
      }
    }
  },
  DEL_CACHED_VIEW: (state, view) => {
    for (const i of state.cachedViews) {
      if (i === view.name || i === view.meta.tagtitle) {
        const index = state.cachedViews.indexOf(i)
        state.cachedViews.splice(index, 1)
        break
      }
    }
  },
  // DEL_CACHED_VIEW: (state, view) => {
  //   for (const i of state.cachedViews) {
  //     if (i === view.fullPath) {
  //       state.delRoute[0] = view.fullPath
  //       const index = state.cachedViews.indexOf(i)
  //       state.cachedViews.splice(index, 1)
  //       break
  //     }
  //   }
  // },
  EMPTYDELROUTE: (state) => {
    state.delRoute = []
  },

  DEL_OTHERS_VISITED_VIEWS: (state, view) => {
    state.visitedViews = state.visitedViews.filter(v => {
      if (v.fullPath !== view.fullPath) {
        state.delRoute.push(v.fullPath)
        console.log('222222222')
      }
      console.log(state.delRoute)
      return v.meta.affix || v.fullPath === view.fullPath
    })
    // state.visitedViews.forEach(i => {
    //   if (i.fullPath !== view.fullPath) {
    //     state.delRoute.push(i.fullPath)
    //   }
    // })
  },
  DEL_OTHERS_CACHED_VIEWS: (state, view) => {
    for (const i of state.cachedViews) {
      if (i === view.name || i === view.meta.tagtitle) {
        const index = state.cachedViews.indexOf(i)
        state.cachedViews = state.cachedViews.slice(index, index + 1)
        break
      }
    }
  },

  DEL_ALL_VISITED_VIEWS: (state) => {
    // keep affix tags
    state.visitedViews.forEach(el => {
      state.delRoute.push(el.fullPath)
    })
    // console.log(state.delRoute)
    // console.log('打印了state.delRoute')
    const affixTags = state.visitedViews.filter(tag => {
      return tag.meta.affix
    })
    state.visitedViews = affixTags
  },
  DEL_ALL_CACHED_VIEWS: state => {
    state.cachedViews = []
  },

  UPDATE_VISITED_VIEW: (state, view) => {
    for (let v of state.visitedViews) {
      if (v.fullPath === view.fullPath) {
        v = Object.assign(v, view)
        break
      }
    }
  }
}

const actions = {
  addView({ dispatch }, view) {
    dispatch('addVisitedView', view)
    dispatch('addCachedView', view)
  },
  addVisitedView({ commit }, view) {
    commit('ADD_VISITED_VIEW', view)
  },
  addCachedView({ commit }, view) {
    commit('ADD_CACHED_VIEW', view)
  },

  delView({ dispatch, state }, view) {
    return new Promise(resolve => {
      dispatch('delVisitedView', view)
      dispatch('delCachedView', view)
      resolve({
        visitedViews: [...state.visitedViews],
        cachedViews: [...state.cachedViews]
      })
    })
  },
  delVisitedView({ commit, state }, view) {
    return new Promise(resolve => {
      commit('DEL_VISITED_VIEW', view)
      resolve([...state.visitedViews])
    })
  },
  delCachedView({ commit, state }, view) {
    return new Promise(resolve => {
      commit('DEL_CACHED_VIEW', view)
      resolve([...state.cachedViews])
    })
  },

  delOthersViews({ dispatch, state }, view) {
    return new Promise(resolve => {
      dispatch('delOthersVisitedViews', view)
      dispatch('delOthersCachedViews', view)
      resolve({
        visitedViews: [...state.visitedViews],
        cachedViews: [...state.cachedViews]
      })
    })
  },
  delOthersVisitedViews({ commit, state }, view) {
    return new Promise(resolve => {
      commit('DEL_OTHERS_VISITED_VIEWS', view)
      resolve([...state.visitedViews])
    })
  },
  delOthersCachedViews({ commit, state }, view) {
    return new Promise(resolve => {
      commit('DEL_OTHERS_CACHED_VIEWS', view)
      resolve([...state.cachedViews])
    })
  },

  delAllViews({ dispatch, state }, view) {
    return new Promise(resolve => {
      dispatch('delAllVisitedViews', view)
      dispatch('delAllCachedViews', view)
      resolve({
        visitedViews: [...state.visitedViews],
        cachedViews: [...state.cachedViews]
      })
    })
  },
  delAllVisitedViews({ commit, state }) {
    return new Promise(resolve => {
      commit('DEL_ALL_VISITED_VIEWS')
      resolve([...state.visitedViews])
    })
  },
  delAllCachedViews({ commit, state }) {
    return new Promise(resolve => {
      commit('DEL_ALL_CACHED_VIEWS')
      resolve([...state.cachedViews])
    })
  },

  updateVisitedView({ commit }, view) {
    commit('UPDATE_VISITED_VIEW', view)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}