feature[Vuex]: auto import vuex modules(#1815)

This commit is contained in:
Estelle00 2019-04-04 14:37:01 +08:00 committed by 花裤衩
parent c923726464
commit e363c7a77b
5 changed files with 30 additions and 18 deletions

View File

@ -24,11 +24,24 @@ const messages = {
...elementEsLocale ...elementEsLocale
} }
} }
export function getLanguage() {
const chooseLanguage = Cookies.get('language')
if (chooseLanguage) return chooseLanguage
// if has not choose language
const language = (navigator.language || navigator.browserLanguage).toLowerCase()
const locales = Object.keys(messages)
for (const locale of locales) {
if (language.indexOf(locale) > -1) {
return locale
}
}
return 'en'
}
const i18n = new VueI18n({ const i18n = new VueI18n({
// set locale // set locale
// options: en | zh | es // options: en | zh | es
locale: Cookies.get('language') || 'en', locale: getLanguage(),
// set locale messages // set locale messages
messages messages
}) })

View File

@ -1,24 +1,24 @@
import Vue from 'vue' import Vue from 'vue'
import Vuex from 'vuex' import Vuex from 'vuex'
import app from './modules/app'
import errorLog from './modules/errorLog'
import permission from './modules/permission'
import tagsView from './modules/tagsView'
import settings from './modules/settings'
import user from './modules/user'
import getters from './getters' import getters from './getters'
Vue.use(Vuex) Vue.use(Vuex)
// https://webpack.js.org/guides/dependency-management/#requirecontext
const modulesFiles = require.context('./modules', false, /\.js$/)
// you do not need `import app from './modules/app'`
// it will auto require all vuex module from modules file
const modules = modulesFiles.keys().reduce((modules, modulePath) => {
// set './app.js' => 'app'
const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
const value = modulesFiles(modulePath)
modules[moduleName] = value.default
return modules
}, {})
const store = new Vuex.Store({ const store = new Vuex.Store({
modules: { modules,
app,
errorLog,
permission,
tagsView,
settings,
user
},
getters getters
}) })

View File

@ -1,4 +1,5 @@
import Cookies from 'js-cookie' import Cookies from 'js-cookie'
import { getLanguage } from '@/lang/index'
const state = { const state = {
sidebar: { sidebar: {
@ -6,7 +7,7 @@ const state = {
withoutAnimation: false withoutAnimation: false
}, },
device: 'desktop', device: 'desktop',
language: Cookies.get('language') || 'en', language: getLanguage(),
size: Cookies.get('size') || 'medium' size: Cookies.get('size') || 'medium'
} }

View File

@ -1,4 +1,3 @@
const state = { const state = {
logs: [] logs: []
} }

View File

@ -1,4 +1,3 @@
const state = { const state = {
visitedViews: [], visitedViews: [],
cachedViews: [] cachedViews: []