从零开始搭建企业级前端项目模板(vue3+vite+ts)
针对 Vue3 + TypeScript + Vite 项目中遇到的“找不到模块“./App.vue”或其相应的类型声明”的问题,即使路径正确但仍然无法构建(npm run build),可以尝试以下解决方案:
第4,5步弄好之后我又去build,报了个
最后又安装了这个依赖 npm i terser,再去build终于成功了。。。
vue2 我使用的是这个
配置改了些:
vue3的可以用 vue3js.cn/
Vue3.0的新语法糖-script setup
zhuanlan.zhihu.com/p/471806345…
zhuanlan.zhihu.com/p/545381243
request.js
import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import store from '@/store'
import { getToken } from '@/utils/auth'
// create an axios instance
const service = axios.create({
// baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
baseURL: "",
// withCredentials: true, // send cookies when cross-domain requests
// timeout: 10*1000 // request timeout
headers: {
'Content-Type': 'application/json'
}
})
// request interceptor
service.interceptors.request.use(
config => {
// do something before request is sent
if (store.getters.token) {
// let each request carry token
// ['X-Token'] is a custom headers key
// please modify it according to the actual situation
config.headers['X-Token'] = getToken()
}
return config
},
error => {
// do something with request error
console.log(error) // for debug
return Promise.reject(error)
}
)
// response interceptor
service.interceptors.response.use(
/**
* If you want to get http information such as headers or status
* Please return response => response
*/
/**
* Determine the request status by custom code
* Here is just an example
* You can also judge the status by HTTP Status Code
*/
response => {
const res = response.data
// if the custom code is not 20000, it is judged as an error.
if (res.status !== 200) {
Message({
message: res.message || 'Error',
type: 'error',
duration: 5 * 1000
})
// 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
if (res.status === 50008 || res.status === 50012 || res.status === 50014) {
// to re-login
MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
confirmButtonText: 'Re-Login',
cancelButtonText: 'Cancel',
type: 'warning'
}).then(() => {
store.dispatch('user/resetToken').then(() => {
location.reload()
})
})
}
return Promise.reject(new Error(res.message || 'Error'))
} else {
return res
}
},
error => {
console.log('err' + error) // for debug
if (error.response) {
// Message({
// message: `请求错误 ${error.response.status}: ${error.response.data.message || 'Error'}`,
// type: 'error',
// duration: 5 * 1000
// })
// Handle specific HTTP status codes, such as 400
// return error.response.data
// return error.response.data // 这里如果直接返回这个 在调接口时就能拿到报错信息 但是这里我还是统一处理状态报错的提示
// 统一处理当error.response.status === 401需要跳回到首页的时候,这里拿不到this.$router,所以还是返回这个报错信息,接口里处理
if (error.response.status === 400) {
// Show the error message from the server or a default one
Message({
message: error.response.data.message,
type: 'error',
duration: 5 * 1000
})
}
if(error.response.status === 401){
return error.response.data
// Message({
// message: error.response.data.message,
// type: 'error',
// duration: 5 * 1000
// })
// this.$router.push({ path: '/' }) // 这里拿不到$router
// localStorage.removeItem('token')
// window.location.reload()
}
}else
if (error.request) {
// The request was made but no response was received
Message({
message: '请求超时或网络异常,请稍后重试',
type: 'error',
duration: 5 * 1000
})
} else {
// Something happened in setting up the request that triggered an Error
Message({
message: '未知错误',
type: 'error',
duration: 5 * 1000
})
}
return Promise.reject(error)
}
)
export default service
// 当状态码是401时需要单独处理,跳回登录页
接口写法api
import request from '@/utils/request'
let token = localStorage.getItem('token')
export function getUnusedProduct(data) {
return request({
url: '/report/unusedProduct',
method: 'post',
data,
headers: {
'token': token
}
})
}
export function getUnusedProductTotal(data) {
return request({
url: '/report/unusedProductTotal',
method: 'post',
data,
headers: {
'token': token
}
})
}
调用api