简体   繁体   English

Vue.js(Vue资源)应用程序结构问题与发送HTTP请求

[英]Vue.js (vue-resource) app structure problems with sending HTTP request

My Vue.js app structure is like so: 我的Vue.js应用程序结构如下:

/app
    --assets
    --components
        --dashboard.vue
    --filters
    --views
        --dashboard-view.vue
    app.vue
    main.js

In my main.js file I have the snippet of code below to set up vue-resource to make HTTP requests: 在我的main.js文件中,我具有下面的代码段来设置vue-resource以发出HTTP请求:

Vue.use(require('vue-resource'))

Vue.http.options.root = 'http://localhost:3000/api/v1/'
Vue.http.headers.common['Authorization'] = 'Bearer ' + localStorage.getItem('authentication_token')

However, when I try to make an HTTP request in dashboard-view.vue it doesn't listen to the configuration set in main.js . 但是,当我尝试在dashboard-view.vue发出HTTP请求时,它不会监听main.js设置的配置。

This is dashboard-view.vue : 这是dashboard-view.vue

<template>
...
</template>

<script>
module.exports = {
  ready: function() {
    this.$http.get('/me', function(data, status, request) {

    })
  }
}
</script>

I'm quite new to Vue.js so help is appreciated on how to structure my app so I can easily send HTTP requests. 我对Vue.js相当陌生,因此非常感谢您提供有关如何构建我的应用程序的帮助,以便我可以轻松发送HTTP请求。

Thanks 谢谢

I was having this same issue. 我遇到了同样的问题。 When vue-resource concatenates the root option with the url parameter, it adds a backslash between them by default. 当vue-resource将root选项与url参数连接在一起时,默认情况下会在它们之间添加反斜杠。 If you already have a backslash at the end of your root option or at the beginning of any of your url parameters, it will just skip the concatenation and default to the current root url. 如果您在root选项的末尾或任何url参数的开头已经有一个反斜杠,则它将跳过连接并默认为当前的根url。

Just change your root option declaration to: 只需将您的root选项声明更改为:

Vue.http.options.root = 'http://localhost:3000/api/v1'

and also change your call in the vue component to: 并将您在vue组件中的呼叫更改为:

<template>
...
</template>

<script>
module.exports = {
  ready: function() {
    this.$http.get('me', function(data, status, request) {

    })
  }
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM