建立项目文件 文件目录下cmd => 运行 npm create vite@latest => 选择vue => 选择js or ts 基础组件下载
npm install vue-router@4 -- savenpm install element-plus -- savenpm install vuex@next -- savenpm install mitt -- save / / 组件通信npm install axiosnpm install vite-plugin-html - D / / 处理html模板插件npm install - D sass / / CSS预处理器
App.vue
< script setup >
</ script> < template> < div id = " app" > < router-view /> </ div>
</ template> < style scoped >
</ style>
src/router/index.js
import { createRouter, createWebHistory } from 'vue-router'
const router = createRouter ( { history : createWebHistory ( import . meta. env. BASE_URL ) , routes : [ { path : '/' , name : 'home' , component : ( ) => import ( '../views/OneMap.vue' ) , } , ]
} ) export default router
src/store/index.js
import { createStore } from 'vuex' export default createStore ( { state : { timeLineTime : '' , } , getters : { } , mutations : { } , actions : { } , modules : { }
} )
src/utils/mitt.js
import mitt from 'mitt' const bus = mitt ( )
export default bus
src/utils/request.js
import axios from 'axios' import router from '@/router/index.js'
const baseURL = "http://192.168.1.8:8086" const instance = axios. create ( { baseURL, timeout : 100000
} )
instance. interceptors. request. use ( ( config ) => { return config} , ( err ) => Promise. reject ( err)
)
instance. interceptors. response. use ( ( res ) => { console. log ( res) if ( res. data. code === 0 ) { return res} ElMessage. error ( res. data. message || '服务异常' ) return Promise. reject ( res. data) } , ( err ) => { console. log ( err) if ( err. code === 401 ) { router. push ( '/' ) } ElMessage. error ( err. message || '服务异常' ) return Promise. reject ( err) }
) export default instance
export { baseURL }
main.js
import './assets/main.css' import { createApp } from 'vue'
import App from './App.vue' import router from './router'
import store from './store' import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import zhCn from 'element-plus/es/locale/lang/zh-cn' const app = createApp ( App) for ( const [ key, component] of Object. entries ( ElementPlusIconsVue) ) { app. component ( key, component) } app. use ( ElementPlus, { locale : zhCn} ) app. use ( store) . use ( router) . use ( ElementPlus) . mount ( '#app' )
vite.config.js
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { createHtmlPlugin } from "vite-plugin-html" const getTarget = ( mode, target ) => { return loadEnv ( mode, process. cwd ( ) ) [ target] ;
} ;
export default defineConfig ( ( { mode } ) => { return { plugins : [ vue ( ) , createHtmlPlugin ( { inject : { data : { title : getTarget ( mode, "VITE_APP_TITLE" ) , } , } , } ) , ] , server : { host : '0.0.0.0' , port : 8991 , https : false , } , }
} )
index.html
<%- title %>
.env.development
# 页面标题
VITE_APP_TITLE = 环境项目名称# 开发环境配置
ENV = 'development' # npm_config_port = 9028 VITE_APP_BASE_API = ''
.env.production
# 页面标题
VITE_APP_TITLE = 生产环境项目名称# 生产环境配置
ENV = 'production'# VITE_APP_BASE_API = ''
.env.staging
# 页面标题
VITE_APP_TITLE = 测试环境项目名称NODE_ENV = production# 测试环境配置
ENV = 'staging' VITE_APP_BASE_API = '/stage-api'