From 68047d343657a940da1fa1597bf4863469b0047e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=BC=BA?= <1206709430@qq.com> Date: Thu, 22 Jun 2023 08:48:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=8F=98=E5=8C=96:=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=8F=92=E4=BB=B6=E5=8A=A8=E6=80=81=E5=AF=BC?= =?UTF-8?q?=E5=85=A5store?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/store/index.js | 10 +++++----- web/src/views/plugins/index.js | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/web/src/store/index.js b/web/src/store/index.js index 66c6f3f..69029dd 100644 --- a/web/src/store/index.js +++ b/web/src/store/index.js @@ -2,11 +2,11 @@ import Vue from 'vue' import Vuex from 'vuex' import d2admin from './modules/d2admin' - +import { getStoreModules } from '@/views/plugins' Vue.use(Vuex) - +// 创建一个空的modules对象 +const modules = { d2admin: d2admin } +Object.assign(modules, getStoreModules()) export default new Vuex.Store({ - modules: { - d2admin - } + modules }) diff --git a/web/src/views/plugins/index.js b/web/src/views/plugins/index.js index 48e5b28..f4116b3 100644 --- a/web/src/views/plugins/index.js +++ b/web/src/views/plugins/index.js @@ -78,3 +78,39 @@ export const plugins = async function install (Vue, options) { window.pluginsAll = components return components } + +export const getStoreModules = function (Vue, options) { + // 获取每个插件的Store文件并进行注册 + if (window.storeModules) return + const storeModules = {} + let components = [] + components = components.concat(importAll(require.context('./', true, /index\.js$/))) + components = components.concat(importAll(require.context('@great-dream/', true, /index\.js$/))) + components = Array.from(new Set(components)) + components.filter(async (key, index) => { + try { + const Module = require('@/views/plugins/' + key + '/src/store/index.js') + // 注册组件 + if (Module.default) { + storeModules[key] = Module.default + console.log(`[${key}]store注册成功`) + return true + } + return false + } catch (exception) { + try { + const Module = require('@great-dream/' + key + '/src/store/index.js') + // 注册组件 + if (Module.default) { + storeModules[key] = Module.default + return true + } + return false + } catch (exception) { + return false + } + } + }) + window.storeModules = storeModules + return storeModules +}