问题:
后端解决了跨域的前提下,前端将ip地址写成相对路径,后端部署时可自行配置
步骤:
一、在index.html同级目录下新建config.js文件
二、在config.js种配置ip,需要将请求地址挂在在windows下配置成全局变量:
window.config = {ip: 'http://10.111.1.129:1008'}
三、在index.html中通过相对路径引入这个ip
<script src="./config.js"></script>
四、因为ip需要多次使用,代码重复,所以配置全局ip变量,在main.js中:
import axios from 'axios'
const instance = axios.create({
baseURL: window.config.ip,
timeout: 1000,
});
Vue.use(VueAxios,instance);
五、在页面中即可直接使用配置好的ip请求数据
methods: {
getList() {
this.axios.get(`/case/video`).then(res => {
if (res.data.length > 0) {
this.list = res.data;
this.clickVideo(0,this.videoList[0].deviceId)
}
else {
this.$message({
message: "列表暂无数据",
type: "warning"
})
}
})
}
}
Tips:
以上只适用于简单的项目,如果时大的项目,还需要进行更多的封装,喵喵喵