Jokcy 老师,遇到一个问题,查了些文档还是没能解决,求助一下。
本地 npm run dev 能正常请求api并获取接口返回数据
npm run build 之后, serve -s dist 接口返回不在是json 数据 而是 html 字符串
axios 文件
import axios from 'axios/dist/axios'
const instance = axios.create({
timeout: 10000,
});
// 请求拦截
instance.interceptors.request.use((config) => {
// NProgress.start(); // start progress bar
config.headers['accessToken'] = ''
return config
})
// 响应拦截
instance.interceptors.response.use(
(res) => {
let data = res
if (res.data) {
data = res.data
}
if (res.data.data) {
data = res.data.data
}
let status = Number(res.status) || 200
let message = data.message || 'Network error'
if (status === 401) {
// TODO: 登录
}
// 如果请求为非200否者默认统一处理
if (status !== 200) {
return Promise.reject(message)
}
return Promise.resolve(data)
},
(error) => {
return Promise.reject(error)
},
)
export default instance
vite.config.js 配置文件
import {
defineConfig
} from 'vite'
import vue from '@vitejs/plugin-vue'
import styleImport from 'vite-plugin-style-import'
import pxtorem from 'postcss-pxtorem'
import commonjs from '@rollup/plugin-commonjs'
import nodeResolve from '@rollup/plugin-node-resolve'
const {
resolve
} = require('path')
// https://vitejs.dev/config/
export default defineConfig({
resolve: {
alias: {
'@': resolve(__dirname, 'src'),
'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js',
},
},
plugins: [
vue(),
nodeResolve(),
commonjs(),
styleImport({
libs: [{
libraryName: 'vant',
esModule: true,
resolveStyle: (name) => `vant/es/${name}/style`,
}, ],
}),
],
css: {
postcss: {
plugins: [
pxtorem({
rootValue: 37.5,
propList: ['*'],
}),
],
},
},
server: {
host: '127.0.0.1',
port: 3002,
proxy: {
'/v3': {
target: 'https://domain.net',
changeOrigin: true
}
}
},
build: {
target: 'es2021',
assetsDir: 'assets',
// rollupOptions: {
// external: [
// 'axios'
// ]
// }
},
})