From e363c7a77b50036e8c22125a9a889a707a859cdc Mon Sep 17 00:00:00 2001 From: Estelle00 <530908773@qq.com> Date: Thu, 4 Apr 2019 14:37:01 +0800 Subject: [PATCH] feature[Vuex]: auto import vuex modules(#1815) --- src/lang/index.js | 15 ++++++++++++++- src/store/index.js | 28 ++++++++++++++-------------- src/store/modules/app.js | 3 ++- src/store/modules/errorLog.js | 1 - src/store/modules/tagsView.js | 1 - 5 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/lang/index.js b/src/lang/index.js index 7c9c8af6..c443f41b 100644 --- a/src/lang/index.js +++ b/src/lang/index.js @@ -24,11 +24,24 @@ const messages = { ...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({ // set locale // options: en | zh | es - locale: Cookies.get('language') || 'en', + locale: getLanguage(), // set locale messages messages }) diff --git a/src/store/index.js b/src/store/index.js index 4842ea7e..70736d3a 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1,24 +1,24 @@ import Vue from 'vue' 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' 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({ - modules: { - app, - errorLog, - permission, - tagsView, - settings, - user - }, + modules, getters }) diff --git a/src/store/modules/app.js b/src/store/modules/app.js index 84875dd6..73616469 100644 --- a/src/store/modules/app.js +++ b/src/store/modules/app.js @@ -1,4 +1,5 @@ import Cookies from 'js-cookie' +import { getLanguage } from '@/lang/index' const state = { sidebar: { @@ -6,7 +7,7 @@ const state = { withoutAnimation: false }, device: 'desktop', - language: Cookies.get('language') || 'en', + language: getLanguage(), size: Cookies.get('size') || 'medium' } diff --git a/src/store/modules/errorLog.js b/src/store/modules/errorLog.js index c97d452a..8e3db060 100644 --- a/src/store/modules/errorLog.js +++ b/src/store/modules/errorLog.js @@ -1,4 +1,3 @@ - const state = { logs: [] } diff --git a/src/store/modules/tagsView.js b/src/store/modules/tagsView.js index 5cbe32f3..3e2c1703 100644 --- a/src/store/modules/tagsView.js +++ b/src/store/modules/tagsView.js @@ -1,4 +1,3 @@ - const state = { visitedViews: [], cachedViews: []