feat:新增全局默认参数配置

This commit is contained in:
张益铭 2021-03-23 17:43:53 +08:00
parent 01c8619124
commit 0ee38c24c8
7 changed files with 68 additions and 12 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "vue-ts",
"version": "0.1.0",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

@ -1,6 +1,6 @@
{
"name": "vue-ts",
"version": "0.1.0",
"version": "1.0.0",
"private": true,
"scripts": {
"serve": "vite",

3
public/serverConfig.json Normal file
View File

@ -0,0 +1,3 @@
{
"version": "1.0.0"
}

View File

@ -112,6 +112,7 @@ export default defineComponent({
onBeforeMount(() => {
vm = getCurrentInstance(); //
console.log('vm: ', vm.appContext.config.globalProperties.$config);
});
//

25
src/config/index.ts Normal file
View File

@ -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 }

View File

@ -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<any> => {
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')

View File

@ -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<any>