Axios
vue本身是不支持发送ajax请求,需要通过其它库来实现(vue1.0----->vue-resource、vue2.0---->axios),或者也可以通过使用jQuery来发送ajax请求。
axios是一个基于promise的HTTP请求客户端,用来发送请求
安装
npm install axios -s
使用
在main.js中引入axios
import axios from 'axios'
Vue.prototype.axios = axios
在组件中使用
data(){
returen{
info:[]
}
},
mounted(){
//第一种方法
this.axios.get('./static/data.json').then((result)=>{
this.info = result.data
}).catch((err)=>{
console.log(err);
});
//第二种方法
this.axios({
method:'GET',
url:'./static/data.json',
data:{},
}).then((res)=>{
console.log(res);
this.info = res.data
}).catch((err)=>{
console.log(err)
})
}
axios 访问本地数据采坑点
在vue-cli项目中,我们静态资源只能放在static文件夹中,axios使用get请求。