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

Thank You For Your Time!

我想将接口都模块化出来

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);
    })
}

报错说this没有定义。我不知道在car.js中,this指向的是什么?也许是vue-resource?


Thanks for your help!

阅读 2.2k
1 个回答

在vue的方法里面,调用this.$http,this是指向vue组件的。在vue方法里面调用了不是vue组件内部的方法,this是不能指向到vue组件本身的。有几种思路:

car.js改写成这样

import axios from 'axios'

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

getCarList方法放置到vue组件内部的method里面去

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


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

以上两种方法

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题