npm i @types/node -D
import { resolve } from 'path'
resolve: {
// 配置别名
alias: {
'@': resolve(__dirname, 'src'),
components: resolve(__dirname, 'src/components')
},
// 类型: string[] 导入时想要省略的扩展名列表。
extensions: ['.js', '.ts', '.jsx', '.tsx', '.json', '.vue', '.mjs']
}
npm i vue-router@4
<template>
<h1>欢迎vite+vue3+ts+pinia+element-plus+qiankun项目h1>
template>
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Home from '@/views/index.vue'
const routes: Array<RouteRecordRaw> = [
{
path: '/',
name: 'Home页面',
component: Home
}
]
const router = createRouter({
history: createWebHistory(),
routes
})
export default router
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
const app = createApp(App)
// 注册路由
app.use(router)
app.mount('#app')
<template>
<router-view />
template>
npm install element-plus @element-plus/icons-vue --save
我这是全量引入;若是按需引入需要unplugin-vue-components插件,请自行配置
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import locale from 'element-plus/lib/locale/lang/zh-cn'// 因element-plus默认是英文,我们指定一下默认中文
// 图标并进行全局注册
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
// 注册ElementPlus
app.use(ElementPlus, {
locale // 语言设置
// size: Cookies.get('size') || 'medium' // 设置默认尺寸
})
// 注册所有图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
app.component(key, component)
}
npm install -D sass sass-loader
npm install axios --save
VITE_APP_ENV='dev' // /sit/uat/prod
# import.meta.env.{参数名}
# api地址
VITE_APP_BASE_API = '' // sit-api/uat-api/prod-api
"scripts": {
"serve": "vite --mode dev",
"dev": "vue-tsc --noEmit && vite build --mode dev",
"sit": "vue-tsc --noEmit && vite build --mode sit",
"uat": "vue-tsc --noEmit && vite build --mode uat",
"prod": "vue-tsc --noEmit && vite build --mode prod",
"preview": "vite preview"
},
npm i @types/js-cookie js-cookie -D
import axios from 'axios'
import Message from '@/utils/message' // 防止重复点击重复弹出message弹框
import { ElMessageBox } from 'element-plus'
import { getToken } from '@/utils/cookies'
export default (config: any) => {
// 创建axios实例
const service: any = axios.create({
baseURL: import.meta.env.VITE_APP_BASE_API as string,
// 超时
timeout: 50000
})
// 请求拦截器
service.interceptors.request.use(
(config: any) => {
config.headers['Authorization'] = getToken() || ''
config.headers['Content-Type'] =
config.headers['Content-Type'] || 'application/json'
// 8080
if (config.type == 'file') {
config.headers['content-type'] = 'application/multipart/form-data'
} else if (config.type == 'form') {
config.headers['Content-type'] = 'application/x-www-form-urlencoded'
}
if (config.method && config.method.toLowerCase() === 'get') {
config.data = true
}
return config
},
(error: any) => {
return Promise.reject(error)
}
)
// 响应拦截器
service.interceptors.response.use(
(response: any) => {
const code = response.data.code
if (code === 401) {
ElMessageBox.confirm(
'登录状态已过期,您可以继续留在该页面,或者重新登录',
'系统提示',
{
confirmButtonText: '重新登录',
cancelButtonText: '取消',
type: 'warning'
}
).then(() => {
// 调用退出登录接口
})
} else if (code !== 200) {
Message({
message: response.data.msg,
type: 'error',
duration: 5 * 1000
})
return Promise.reject('error')
} else {
return response.data
}
},
(error: any) => {
Message({
message: error.message,
type: 'error',
duration: 5 * 1000
})
return Promise.reject(error)
}
)
return service(config)
}
import request from '@/utils/request'
import * as masterData from './mes/masterData' // 业务接口
// 数据字典-查询 公共api
export const getDicts = (dictType: any) => { return request({ url: `/system/dict/data/type/${dictType}`, method: 'GET' }) }
export default {
getDicts,
...masterData
}
// 所有业务api接口
import api from './api'
// 注册全局api方法
app.config.globalProperties.$api = api
<script setup lang="ts">
import { getCurrentInstance } from 'vue'
const { proxy } = getCurrentInstance() as any
// 获取列表数据
const getList = async () => {
const res = await proxy.$api.materialList()
console.log('proxy', res)
if (res.success) {
console.log('获取接口数据', res.data.records)
}
}
getList()
</script>
// 解析非相对模块名的基准目录
"baseUrl": "./",
// 模块名到基于 baseUrl的路径映射的列表。
"paths": {
"@": [
"src"
],
"@/*": [
"src/*"
]
},