使用Pinia代替Vuex进行数据存储
parent
6f359c1534
commit
44ad220cdd
@ -1,18 +0,0 @@
|
|||||||
const getters = {
|
|
||||||
sidebar: state => state.app.sidebar,
|
|
||||||
size: state => state.app.size,
|
|
||||||
device: state => state.app.device,
|
|
||||||
visitedViews: state => state.tagsView.visitedViews,
|
|
||||||
cachedViews: state => state.tagsView.cachedViews,
|
|
||||||
token: state => state.user.token,
|
|
||||||
avatar: state => state.user.avatar,
|
|
||||||
name: state => state.user.name,
|
|
||||||
introduction: state => state.user.introduction,
|
|
||||||
roles: state => state.user.roles,
|
|
||||||
permissions: state => state.user.permissions,
|
|
||||||
permission_routes: state => state.permission.routes,
|
|
||||||
topbarRouters:state => state.permission.topbarRouters,
|
|
||||||
defaultRoutes:state => state.permission.defaultRoutes,
|
|
||||||
sidebarRouters:state => state.permission.sidebarRouters,
|
|
||||||
}
|
|
||||||
export default getters
|
|
@ -1,21 +1,3 @@
|
|||||||
import { createStore } from 'vuex'
|
const store = createPinia()
|
||||||
import app from './modules/app'
|
|
||||||
import user from './modules/user'
|
|
||||||
import tagsView from './modules/tagsView'
|
|
||||||
import permission from './modules/permission'
|
|
||||||
import settings from './modules/settings'
|
|
||||||
import getters from './getters'
|
|
||||||
|
|
||||||
const store = createStore({
|
|
||||||
modules: {
|
|
||||||
app,
|
|
||||||
user,
|
|
||||||
tagsView,
|
|
||||||
permission,
|
|
||||||
settings
|
|
||||||
},
|
|
||||||
getters
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
export default store
|
export default store
|
@ -1,207 +1,153 @@
|
|||||||
const state = {
|
const useTagsViewStore = defineStore(
|
||||||
|
'tags-view',
|
||||||
|
{
|
||||||
|
state: () => ({
|
||||||
visitedViews: [],
|
visitedViews: [],
|
||||||
cachedViews: []
|
cachedViews: []
|
||||||
}
|
}),
|
||||||
|
actions: {
|
||||||
const mutations = {
|
addView(view) {
|
||||||
ADD_VISITED_VIEW: (state, view) => {
|
this.addVisitedView(view)
|
||||||
if (state.visitedViews.some(v => v.path === view.path)) return
|
this.addCachedView(view)
|
||||||
state.visitedViews.push(
|
},
|
||||||
|
addVisitedView(view) {
|
||||||
|
if (this.visitedViews.some(v => v.path === view.path)) return
|
||||||
|
this.visitedViews.push(
|
||||||
Object.assign({}, view, {
|
Object.assign({}, view, {
|
||||||
title: view.meta.title || 'no-name'
|
title: view.meta.title || 'no-name'
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
ADD_CACHED_VIEW: (state, view) => {
|
addCachedView(view) {
|
||||||
if (state.cachedViews.includes(view.name)) return
|
if (this.cachedViews.includes(view.name)) return
|
||||||
if (!view.meta.noCache) {
|
if (!view.meta.noCache) {
|
||||||
state.cachedViews.push(view.name)
|
this.cachedViews.push(view.name)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
delView(view) {
|
||||||
DEL_VISITED_VIEW: (state, view) => {
|
return new Promise(resolve => {
|
||||||
for (const [i, v] of state.visitedViews.entries()) {
|
this.delVisitedView(view)
|
||||||
|
this.delCachedView(view)
|
||||||
|
resolve({
|
||||||
|
visitedViews: [...this.visitedViews],
|
||||||
|
cachedViews: [...this.cachedViews]
|
||||||
|
})
|
||||||
|
})
|
||||||
|
},
|
||||||
|
delVisitedView(view) {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
for (const [i, v] of this.visitedViews.entries()) {
|
||||||
if (v.path === view.path) {
|
if (v.path === view.path) {
|
||||||
state.visitedViews.splice(i, 1)
|
this.visitedViews.splice(i, 1)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
resolve([...this.visitedViews])
|
||||||
|
})
|
||||||
|
},
|
||||||
|
delCachedView(view) {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
const index = this.cachedViews.indexOf(view.name)
|
||||||
|
index > -1 && this.cachedViews.splice(index, 1)
|
||||||
|
resolve([...this.cachedViews])
|
||||||
|
})
|
||||||
},
|
},
|
||||||
DEL_CACHED_VIEW: (state, view) => {
|
delOthersViews(view) {
|
||||||
const index = state.cachedViews.indexOf(view.name)
|
return new Promise(resolve => {
|
||||||
index > -1 && state.cachedViews.splice(index, 1)
|
this.delOthersVisitedViews(view)
|
||||||
|
this.delOthersCachedViews(view)
|
||||||
|
resolve({
|
||||||
|
visitedViews: [...this.visitedViews],
|
||||||
|
cachedViews: [...this.cachedViews]
|
||||||
|
})
|
||||||
|
})
|
||||||
},
|
},
|
||||||
|
delOthersVisitedViews(view) {
|
||||||
DEL_OTHERS_VISITED_VIEWS: (state, view) => {
|
return new Promise(resolve => {
|
||||||
state.visitedViews = state.visitedViews.filter(v => {
|
this.visitedViews = this.visitedViews.filter(v => {
|
||||||
return v.meta.affix || v.path === view.path
|
return v.meta.affix || v.path === view.path
|
||||||
})
|
})
|
||||||
|
resolve([...this.visitedViews])
|
||||||
|
})
|
||||||
},
|
},
|
||||||
DEL_OTHERS_CACHED_VIEWS: (state, view) => {
|
delOthersCachedViews(view) {
|
||||||
const index = state.cachedViews.indexOf(view.name)
|
return new Promise(resolve => {
|
||||||
|
const index = this.cachedViews.indexOf(view.name)
|
||||||
if (index > -1) {
|
if (index > -1) {
|
||||||
state.cachedViews = state.cachedViews.slice(index, index + 1)
|
this.cachedViews = this.cachedViews.slice(index, index + 1)
|
||||||
} else {
|
} else {
|
||||||
state.cachedViews = []
|
this.cachedViews = []
|
||||||
}
|
}
|
||||||
|
resolve([...this.cachedViews])
|
||||||
|
})
|
||||||
},
|
},
|
||||||
|
delAllViews(view) {
|
||||||
DEL_ALL_VISITED_VIEWS: state => {
|
return new Promise(resolve => {
|
||||||
// keep affix tags
|
this.delAllVisitedViews(view)
|
||||||
const affixTags = state.visitedViews.filter(tag => tag.meta.affix)
|
this.delAllCachedViews(view)
|
||||||
state.visitedViews = affixTags
|
resolve({
|
||||||
|
visitedViews: [...this.visitedViews],
|
||||||
|
cachedViews: [...this.cachedViews]
|
||||||
|
})
|
||||||
|
})
|
||||||
},
|
},
|
||||||
DEL_ALL_CACHED_VIEWS: state => {
|
delAllVisitedViews(view) {
|
||||||
state.cachedViews = []
|
return new Promise(resolve => {
|
||||||
|
const affixTags = this.visitedViews.filter(tag => tag.meta.affix)
|
||||||
|
this.visitedViews = affixTags
|
||||||
|
resolve([...this.visitedViews])
|
||||||
|
})
|
||||||
},
|
},
|
||||||
|
delAllCachedViews(view) {
|
||||||
UPDATE_VISITED_VIEW: (state, view) => {
|
return new Promise(resolve => {
|
||||||
for (let v of state.visitedViews) {
|
this.cachedViews = []
|
||||||
|
resolve([...this.cachedViews])
|
||||||
|
})
|
||||||
|
},
|
||||||
|
updateVisitedView(view) {
|
||||||
|
for (let v of this.visitedViews) {
|
||||||
if (v.path === view.path) {
|
if (v.path === view.path) {
|
||||||
v = Object.assign(v, view)
|
v = Object.assign(v, view)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
delRightTags(view) {
|
||||||
DEL_RIGHT_VIEWS: (state, view) => {
|
return new Promise(resolve => {
|
||||||
const index = state.visitedViews.findIndex(v => v.path === view.path)
|
const index = this.visitedViews.findIndex(v => v.path === view.path)
|
||||||
if (index === -1) {
|
if (index === -1) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
state.visitedViews = state.visitedViews.filter((item, idx) => {
|
this.visitedViews = this.visitedViews.filter((item, idx) => {
|
||||||
if (idx <= index || (item.meta && item.meta.affix)) {
|
if (idx <= index || (item.meta && item.meta.affix)) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
const i = state.cachedViews.indexOf(item.name)
|
const i = this.cachedViews.indexOf(item.name)
|
||||||
if (i > -1) {
|
if (i > -1) {
|
||||||
state.cachedViews.splice(i, 1)
|
this.cachedViews.splice(i, 1)
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
})
|
})
|
||||||
|
resolve([...this.visitedViews])
|
||||||
|
})
|
||||||
},
|
},
|
||||||
|
delLeftTags(view) {
|
||||||
DEL_LEFT_VIEWS: (state, view) => {
|
const index = this.visitedViews.findIndex(v => v.path === view.path)
|
||||||
const index = state.visitedViews.findIndex(v => v.path === view.path)
|
|
||||||
if (index === -1) {
|
if (index === -1) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
state.visitedViews = state.visitedViews.filter((item, idx) => {
|
this.visitedViews = this.visitedViews.filter((item, idx) => {
|
||||||
if (idx >= index || (item.meta && item.meta.affix)) {
|
if (idx >= index || (item.meta && item.meta.affix)) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
const i = state.cachedViews.indexOf(item.name)
|
const i = this.cachedViews.indexOf(item.name)
|
||||||
if (i > -1) {
|
if (i > -1) {
|
||||||
state.cachedViews.splice(i, 1)
|
this.cachedViews.splice(i, 1)
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
|
||||||
},
|
|
||||||
|
|
||||||
delRightTags({ commit }, view) {
|
|
||||||
return new Promise(resolve => {
|
|
||||||
commit('DEL_RIGHT_VIEWS', view)
|
|
||||||
resolve([...state.visitedViews])
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
delLeftTags({ commit }, view) {
|
|
||||||
return new Promise(resolve => {
|
|
||||||
commit('DEL_LEFT_VIEWS', view)
|
|
||||||
resolve([...state.visitedViews])
|
|
||||||
})
|
})
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
export default {
|
export default useTagsViewStore
|
||||||
namespaced: true,
|
|
||||||
state,
|
|
||||||
mutations,
|
|
||||||
actions
|
|
||||||
}
|
|
||||||
|
@ -1,12 +1,14 @@
|
|||||||
import store from '@/store'
|
import store from '@/store'
|
||||||
import defaultSettings from '@/settings'
|
import defaultSettings from '@/settings'
|
||||||
|
import useSettingsStore from '@/store/modules/settings'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 动态修改标题
|
* 动态修改标题
|
||||||
*/
|
*/
|
||||||
export function useDynamicTitle() {
|
export function useDynamicTitle() {
|
||||||
if (store.state.settings.dynamicTitle) {
|
const settingsStore = useSettingsStore();
|
||||||
document.title = store.state.settings.title + ' - ' + defaultSettings.title;
|
if (settingsStore.dynamicTitle) {
|
||||||
|
document.title = settingsStore.title + ' - ' + defaultSettings.title;
|
||||||
} else {
|
} else {
|
||||||
document.title = defaultSettings.title;
|
document.title = defaultSettings.title;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue