浏览器自带的fetch函数:
官方推荐的axios:
mounted(){
this.lastCity=this.city
this.getHomeInfo()
},
methods:{
getHomeInfo(){
axios.get('/api/index.json?city='+this.city)
.then(this.getHomeInfoSucc)
},
methods: {
getImg: function(){
var that = this;
that.$http({ //调用接口
method:'GET',
url:this.getImgUrl //this指data
}).then(function(response){ //接口返回数据
this.imgList=response.data;
},function(error){
})
}
}
使用vue-resource代码如下:
new Vue({
el:'#app',
data:{data:""},
created:function(){
var url="json.jsp";
this.$http.get(url).then(function(data){
var json=data.body;
this.data=eval("(" + json +")");
},function(response){
console.info(response);
})
}
});
使用Jquery的时候:
new Vue({
el:'#app',
data:{data:""},
beforeCreate:function(){
var url="json.jsp";
var _self=this;
$.get(url,function(data){
_self.data=eval("(" + data +")");
})
}
})