【问题标题】:How to make axios's API requests reusable with vuejs?如何使用 vuejs 使 axios 的 API 请求可重用?
【发布时间】:2018-05-29 07:29:13
【问题描述】:

如何通过我的应用使 axios 的 API 请求可重用, 这样我就可以避免一遍又一遍地编写相同的代码..

//load the axios HTTP library
window.axios = require('axios');
window.Vue = require('vue');


const app = new Vue({
    el: '#app',
    mounted(){
        console.log('hello!');
    }
});

我正在考虑尝试这样的事情 ( gist ) 但我不知道如何将函数附加到 Vue 实例 并从vue-components 直接 ..

【问题讨论】:

  • 我已经构建了axios-resource,它可以帮助使用简单的 API 封装常见调用。

标签: vue.js axios


【解决方案1】:

您可以将 axios 作为 Vue 属性注入: Vue.prototype.$http = require('axios'); 然后你可以这样使用它:this.$http.get(...) 或者您可以查看此repository

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    • 1970-01-01
    • 2018-03-28
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多