首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么忽略为vue-resource设置Vue.http?

为什么忽略为vue-resource设置Vue.http?
EN

Stack Overflow用户
提问于 2017-05-25 05:39:08
回答 2查看 1.1K关注 0票数 0

我正在使用Vue.js 2.3.3,Vue Resource 1.3.3,Vue Router 2.5.3,并且我正在尝试设置Vue-Auth。然而,我一直收到一个控制台错误,上面写着auth.js?b7de:487 Error (@websanova/vue-auth): vue-resource.1.x.js : Vue.http must be set.。我在main.js中设置了Vue.http,但是由于某些原因,vue-resource不能使用它。

main.js:

代码语言:javascript
复制
import Vue from 'vue'
import Actions from 'actions'
import App from './App'

Vue.use(Actions, {
  locales: ['en', 'zh', 'fr']
})

Vue.http.options.root = 'https://api.example.com'

new Vue({
  render: h => h(App),
  watch: {
    lang: function (val) {
      Vue.config.lang = val
    }
  }
}).$mount('#app')

操作/index.js

代码语言:javascript
复制
import VueResource from 'vue-resource'
import Router from 'actions/router'
import I18n from 'actions/i18n'

export default {
  install (Vue, options) {
    Vue.use(Router)
    Vue.use(I18n, options.locales)
    Vue.use(require('@websanova/vue-auth'), {
      router: require('@websanova/vue-auth/drivers/router/vue-router.2.x'),
      auth: require('@websanova/vue-auth/drivers/auth/bearer'),
      http: require('@websanova/vue-auth/drivers/http/vue-resource.1.x')
    })
  }
}

如果我将Vue.use(VueResource)添加到Vue.use(Router)下面的actions/index.js中,我会得到一个新的错误:Error (@websanova/vue-auth): vue-router.2.x.js : Vue.router must be set.

另一方面,如果我将Vue.http.options.root = 'https://api.example.com'移到import语句的正下方,我会得到另一个错误:Uncaught TypeError: Cannot read property 'options' of undefined

EN

回答 2

Stack Overflow用户

发布于 2017-05-25 07:47:44

你需要将'vue-resource‘导入到你的main.js文件中来避免这个错误:

代码语言:javascript
复制
import Vue from 'vue'
import VueResource from 'vue-resource';
import Actions from 'actions'
import App from './App'

Vue.use(Actions, {
  locales: ['en', 'zh', 'fr']
})

Vue.use(VueResource)
Vue.http.options.root = 'https://api.example.com'

new Vue({
  render: h => h(App),
  watch: {
    lang: function (val) {
      Vue.config.lang = val
    }
  }
}).$mount('#app')
票数 0
EN

Stack Overflow用户

发布于 2018-08-12 23:16:50

使用axios而不是vue-resource,这对我来说是一个有效的设置:

代码语言:javascript
复制
import axios from 'axios'
import VueAxios from 'vue-axios'

Vue.use(VueRouter)
Vue.use(VueAxios, axios)

Vue.router = new VueRouter({
  // Your routes.
})

Vue.use(require('@websanova/vue-auth'), {
    auth: require('@websanova/vue-auth/drivers/auth/bearer.js'),
    http: require('@websanova/vue-auth/drivers/http/axios.1.x.js'),
    router: require('@websanova/vue-auth/drivers/router/vue-router.2.x.js'),
})

App.router = Vue.router

new Vue(App).$mount('#app')

有关更多指导,请参阅此教程:https://codeburst.io/api-authentication-in-laravel-vue-spa-using-jwt-auth-d8251b3632e0

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44168893

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档