3.vue要求データ
2344 ワード
データを要求する方法: vue-resource公式提供vueのプラグイン axios fetch-jsonp vue-resourceの使用
使用手順:1、
axiosはfetch-jsonpと同様にサードパーティプラグイン1、インストール
1、インストール
使用手順:1、
vue-resource
モジュールの取り付けcnpm install vue-resource --save
2.main.js
にvue-resource
を導入するimport VueResource from 'vue-resource';
Vue.use(VueResource);
3、コンポーネントの中で直接使うthis.$http.get( ).then(function(){
})
例:Info.vue
-
{{item.title}}
export default {
name: "Info",
data() {
return {
list: []
}
},
methods: {
getData: function () {
let api = 'http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1';
// 。
this.$http.get(api).then((res)=>{
this.list = res.body.result;
}, (err)=>{
console.log(err);
});
}
},
mounted() {
this.getData();
}
}
getData()
に矢印関数が適用されない場合は、this
の問題に注意する必要があります.getData: function () {
let api = 'http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1';
const _this = this;
this.$http.get(api).then(function (res) {
_this.list = res.body.result;
}, function (err) {
console.log(err);
});
}
axiosの使用axiosはfetch-jsonpと同様にサードパーティプラグイン1、インストール
cnpm install axios --save
2、どこでaxiosを導入するかAxios.get(api).then((response)=>{
this.list=response.data.result;
}).catch((error)=>{
console.log(error);
})
fetch-jsonpの使用1、インストール
cnpm install fetch-jsonp --save
2、どこでaxiosを導入するかfetchJsonp('/users.jsonp')
.then(function(response) {
return response.json()
}).then(function(json) {
console.log('parsed json', json)
}).catch(function(ex) {
console.log('parsing failed', ex)
})