1、安装axios
npm install axios -s
2、在main.js中引入axios
import axios from 'axios'
Vue.prototype.$http = axios;//给原型链添加方法,使用时this.$http.get("url")
3、使用
data(){
return{
cartList:[]
}
}
mounted(){
// 执行函数
this.init();
},
methods{
//初始化数据
init(){
this.$http.get("/data/cart.json").then((response)=>{
let res =response.data;//获取请求到的数据中的data,即json的内容
this.cartList[] = res.data;//获取json中的data数组
})
}
}
<div v-for="(item,index) in cartList" :key="index">{{item.id}}</div>
附上json:
{
"code":0,
"data":[
{
"id": "1",
"title": "test1",
"content": "test1",
"timestamp": "2019-12-26"
},
{
"id": "2",
"title": "test2",
"content": "test2",
"timestamp": "2019-12-26"
}
],
"message":""
}