1.安装vue-axios
npm install --save axios vue-axios
2.main.js里全局引入vue-axios
import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios);
3.vue.congfig.js里设置proxyTable实现跨域请求
devServer: {
open: true,
host: 'localhost',
port: 8080,
https: false,
hotOnly: false,
// 配置跨域,请求地址为: https://www.apiopen.top/journalismApi
proxy: {
'/api': {
target: 'https://www.apiopen.top',
ws: true,
changOrigin: true,
pathRewrite: {
'^/api': ''
}
}
},
before: app => { }
}
4.获取数据(在Home.vue里获取数据)
//created生命周期函数: 实例已经创建完成之后执行该生命周期函数
created () {
this.getBannerData();
},
methods: {
getBannerData () {
//数据请求地址: https://www.apiopen.top/journalismApi
this.axios.get("/api/journalismApi").then( resolve => {
console.log(resolve)
}).catch( error => [
console.log(error)
])
}
}
.