我在main.js文件中的Vue-Resource中指定了根选项,但是当我执行请求时,它不使用root选项。我错过了什么?
以下是代码:
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
感谢您的帮助。
答案 0 :(得分:50)
哦,这很棘手!
为了考虑root,您需要从网址中删除最初的/
:
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'
有了它,它会起作用!