1. 配置baseUrl
在package.json同级目录下新建一个faceConfig.js文件

faceConfig.js:
// 前端所有配置放这里 const faceConfig = () => { return { // 正式环节 'devServer': window.location.origin + '***' //***为上下文路径 } } module.exports = faceConfig()
2. 在src下新建api文件夹,并在其下面新建index.js文件

index.js:
const API_ROOT = require('../../faceConfig.js').devServer;
export default {
/** 账号密码登录登出 **/
loginIn: `${API_ROOT}/xxx`,
logOut: `${API_ROOT}/xxx`,
/** 数据库 **/
list: `${API_ROOT}/xxx`, //列表
}
3. 组件中使用
<template>
<div>
<button @click="exit">退出</button >
</div>
</template>
<script>
import API from '../api/index.js' //注意路径是否正确
export default {
name: 'Home',
data() {
return {}
},
methods: {
exit() {
let _this = this,
token = localStorage.getItem('token');
_this.$get(`${API.logout}?token=${token}`) //` `是模板字符串,ES2015新增的符号
.then((res) => {
if (res.status == 200 && res.data.code == 1) {
···
} else {
···
}
})
}
}
}
</script>