ajax使用說(shuō)明
## vue ajax庫(kù)
* 2個(gè)常用的庫(kù)
? * vue-resource: vue插件,非官方庫(kù), vue1.x使用廣泛
? * axios:通用的ajax請(qǐng)求庫(kù),官方推薦, vue2.x使用廣泛
* vue-resource基本使用
? *在線文檔: https://github.com/pagekit/vue-resource/blob/develop/docs/http.md
*下載: npm install vue-resource --save
*編碼
? ? ```
//引入模塊
? ? import VueResource from 'vue-resource'
//聲明使用插件
? ? Vue.use(VueResource)
//通過(guò)vue組件對(duì)象發(fā)送ajax請(qǐng)求
? ? this.$http.get('/someUrl').then((response) => {
// success callback
console.log(response.body) //返回結(jié)果數(shù)據(jù)
? ? }, (response) => {
// error callback
console.log(response.statusText) //錯(cuò)誤信息
? ? })
```
* axios基本使用
? * axios在線文檔: https://github.com/mzabriskie/axios
*下載axios: npm install axios --save
*編碼:
```
//引入模塊
? ? import axios from 'axios'
//發(fā)送ajax請(qǐng)求
? ? axios.get(url)
.then((response) => {
console.log(response.data) //得到返回結(jié)果數(shù)據(jù)
? ? ? })