【问题标题】:Vue Resource root options not used?未使用 Vue 资源根选项?
【发布时间】:2016-04-28 23:39:34
【问题描述】:

我在我的 main.js 文件中的 Vue-Resource 中指定了一个根选项,但是当我发出请求时,它不使用根选项。我错过了什么?

代码如下:

main.js:

Vue.http.options.root = 'http://api.domain.com/v1/'

在组件中:

ready: function () {
    console.log(this.$http.options.root) // Correctly show 'http://api.domain.com/v1/'

    this.$http.get('/members/', null, { // FAILS because it tries to load /members/ in the current domain
        headers: {'auth-token': 'abcde'}
    }).then(function (xhr) {
        // process ...
    })
}

我做错了什么?

我正在使用 Vue.js v1.0.15 和 Vue-Resource v0.6.1

感谢您的帮助。

【问题讨论】:

    标签: vue.js vue-resource


    【解决方案1】:

    哦,这很棘手!

    为了考虑到 root,您需要从 url 中删除初始的 /

    this.$http.get('/members/') 变成 this.$http.get('members/')

    另外,您需要删除根目录中的最后一个/

    Vue.http.options.root = 'http://api.domain.com/v1/'
    

    变成

    Vue.http.options.root = 'http://api.domain.com/v1'
    

    有了它,它就会起作用!

    【讨论】:

    • 大声笑,谢谢。我也在苦苦挣扎,为什么 root 不工作。
    猜你喜欢
    • 2018-03-28
    • 2016-07-19
    • 1970-01-01
    • 2017-07-20
    • 2016-03-23
    • 2018-09-08
    • 2019-08-27
    • 2018-07-21
    • 2020-02-04
    相关资源
    最近更新 更多