在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/HTML/ vue-resource怎么抽象在接口中使用?

vue-resource怎么抽象在接口中使用?

Thank You For Your Time!

我想將接口都模塊化出來(lái)

index-page.vue

import {
    getCarList
} from 'api/car.js';


created: function () {
    getCarList();
},

car.js

export function getCarList() {
    let url = domain + '/api/webOldCar/initNominate.action';
    this.$http.get(url).then((res) => {
        console.log(res);
    })
}

報(bào)錯(cuò)說(shuō)this沒(méi)有定義。我不知道在car.js中,this指向的是什么?也許是vue-resource?


Thanks for your help!

回答
編輯回答
清夢(mèng)

在vue的方法里面,調(diào)用this.$http,this是指向vue組件的。在vue方法里面調(diào)用了不是vue組件內(nèi)部的方法,this是不能指向到vue組件本身的。有幾種思路:

car.js改寫(xiě)成這樣

import axios from 'axios'

export function getCarList() {
    let url = domain + '/api/webOldCar/initNominate.action';
    axios.get(url).then((res) => {
        console.log(res);
    })
}

getCarList方法放置到vue組件內(nèi)部的method里面去

import {
    getCarList
} from 'api/car.js';


created: function () {
    this.getCarList();
},
methods: {
    getCarList: getCarList
}

以上兩種方法

2017年7月11日 04:58