Почему игнорируется установка Vue.http для vue-ресурса?

Я использую 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.. Я устанавливаю Vue.http в main.js, но vue-resource по какой-то причине его не забирает.

main.js:

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

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) в actions / index.js прямо под Vue.use(Router), я получу новую ошибку: Error (@websanova/vue-auth): vue-router.2.x.js : Vue.router must be set.

С другой стороны, если я перемещаю Vue.http.options.root = 'https://api.example.com' прямо под операторы импорта, я получаю еще одну ошибку: Uncaught TypeError: Cannot read property 'options' of undefined


person Confused One    schedule 24.05.2017    source источник
comment
Когда я Vue.use(VueResource), я получаю новую ошибку Error (@websanova/vue-auth): vue-router.2.x.js : Vue.router must be set.   -  person Confused One    schedule 25.05.2017


Ответы (2)


Вам нужно импортировать vue-resource в файл main.js, чтобы избавиться от этих ошибок:

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')
person Firmino Changani    schedule 24.05.2017
comment
Импорт vue-resource в файл main.js приводит к той же ошибке: Error (@websanova/vue-auth): vue-resource.1.x.js : Vue.http must be set. - person Confused One; 25.05.2017
comment
Удачи в исправлении этой ошибки? Прошел год, а в интернете нет решений - person Legolas; 28.05.2018

Я использую axios, а не vue-resource, это рабочая установка:

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

person antoni    schedule 12.08.2018