From 0ee38c24c8684ac7ee8f0fa5f5519dd244fcc12e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=9B=8A=E9=93=AD?= Date: Tue, 23 Mar 2021 17:43:53 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E6=96=B0=E5=A2=9E=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E9=BB=98=E8=AE=A4=E5=8F=82=E6=95=B0=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 2 +- package.json | 2 +- public/serverConfig.json | 3 +++ src/components/info/index.vue | 1 + src/config/index.ts | 25 ++++++++++++++++++++++ src/main.ts | 39 +++++++++++++++++++++++++++++++++-- src/utils/loaders/index.ts | 8 ------- 7 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 public/serverConfig.json create mode 100644 src/config/index.ts diff --git a/package-lock.json b/package-lock.json index aa04fbef1..69ebb51ce 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "vue-ts", - "version": "0.1.0", + "version": "1.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index aa3086cc8..a2002bf5d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vue-ts", - "version": "0.1.0", + "version": "1.0.0", "private": true, "scripts": { "serve": "vite", diff --git a/public/serverConfig.json b/public/serverConfig.json new file mode 100644 index 000000000..688e93980 --- /dev/null +++ b/public/serverConfig.json @@ -0,0 +1,3 @@ +{ + "version": "1.0.0" +} \ No newline at end of file diff --git a/src/components/info/index.vue b/src/components/info/index.vue index c3e12f5d6..0b58af692 100644 --- a/src/components/info/index.vue +++ b/src/components/info/index.vue @@ -112,6 +112,7 @@ export default defineComponent({ onBeforeMount(() => { vm = getCurrentInstance(); //获取组件实例 + console.log('vm: ', vm.appContext.config.globalProperties.$config); }); // 点击登录或注册 diff --git a/src/config/index.ts b/src/config/index.ts new file mode 100644 index 000000000..bfe0f1683 --- /dev/null +++ b/src/config/index.ts @@ -0,0 +1,25 @@ +let config: object = {} + +const setConfig = (cfg?: any) => { + config = Object.assign(config, cfg) +} + +const getConfig = (key?: string) => { + if (typeof key === "string") { + const arr = key.split(".") + if (arr && arr.length) { + let data = config + arr.forEach(v => { + if (data && typeof data[v] !== "undefined") { + data = data[v] + } else { + data = null + } + }) + return data + } + } + return config +} + +export { getConfig, setConfig } diff --git a/src/main.ts b/src/main.ts index 0b7149c40..65280489e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -30,6 +30,41 @@ import './style/index.scss' import "./assets/iconfont/iconfont.js" import "./assets/iconfont/iconfont.css" -const app = createApp(App) +import { setConfig, getConfig } from "./config" +import axios from 'axios' + +const app = createApp(App) +app.config.globalProperties.$config = getConfig() + +// 获取项目动态全局配置 +export const getServerConfig = async (): Promise => { + return axios({ + baseURL: "", + method: "get", + url: (app.config.globalProperties.$baseUrl || "/") + "serverConfig.json" + }).then(({ data: config }) => { + let $config = app.config.globalProperties.$config + // 自动注入项目配置 + if (app && $config && typeof config === "object") { + $config = Object.assign($config, config) + app.config.globalProperties.$config = $config + // 设置全局配置 + setConfig($config) + } + // 设置全局baseURL + app.config.globalProperties.$baseUrl = $config.baseURL + return $config + }).catch(() => { throw "请在public文件夹下添加serverConfig.json配置文件" }) +} + +getServerConfig().then(() => { + app + .use(store) + .use(router) + .use(i18n) + .use(ElementPlus) + .use(VXETable) + .mount('#app') +}) + -app.use(store).use(router).use(i18n).use(ElementPlus).use(VXETable).mount('#app') diff --git a/src/utils/loaders/index.ts b/src/utils/loaders/index.ts index 72854ae87..9439f13a3 100644 --- a/src/utils/loaders/index.ts +++ b/src/utils/loaders/index.ts @@ -1,11 +1,3 @@ -/* - * @Author: your name - * @Date: 2021-02-02 15:12:44 - * @LastEditTime: 2021-02-02 15:17:37 - * @LastEditors: Please set LastEditors - * @Description: In User Settings Edit - * @FilePath: \CURD-TS-self\src\utils\loaders\index.ts - */ interface ProxyLoader { loadCss(src: string): any loadScript(src: string): Promise