-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
81 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
import { createWebHashHistory, createRouter } from 'vue-router' | ||
import routes from './routes' | ||
import store from '../store' | ||
|
||
const router = createRouter({ | ||
history: createWebHashHistory(), | ||
routes | ||
}) | ||
|
||
router.beforeEach((to, from, next) => { | ||
store.commit('cachedView/ADD_CACHED_VIEW', to) | ||
next() | ||
}) | ||
|
||
export default router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const getters = { | ||
cachedViews: state => state.cachedView.cachedViews | ||
} | ||
|
||
export default getters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { createStore } from 'vuex' | ||
import getters from './getters' | ||
|
||
// 批量导入 ./modules 目录下的所有模块 | ||
const modulesFiles = require.context('./modules', true, /\.js$/) | ||
const modules = modulesFiles.keys().reduce((modules, modulePath) => { | ||
const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1') | ||
const value = modulesFiles(modulePath) | ||
modules[moduleName] = value.default | ||
return modules | ||
}, {}) | ||
|
||
const store = createStore({ | ||
getters, | ||
modules | ||
}) | ||
|
||
export default store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const state = { | ||
cachedViews: [] | ||
} | ||
|
||
const mutations = { | ||
ADD_CACHED_VIEW: (state, view) => { | ||
// 不重复添加 | ||
if (state.cachedViews.includes(view.name)) return | ||
if (view?.meta?.keepAlive) { | ||
state.cachedViews.push(view.name) | ||
} | ||
}, | ||
DEL_CACHED_VIEW: (state, view) => { | ||
const index = state.cachedViews.indexOf(view.name) | ||
index > -1 && state.cachedViews.splice(index, 1) | ||
}, | ||
DEL_ALL_CACHED_VIEWS: state => { | ||
state.cachedViews = [] | ||
} | ||
} | ||
|
||
export default { | ||
namespaced: true, | ||
state, | ||
mutations | ||
} |