无法在Vue拦截器

时间:2017-04-19 14:11:04

标签: vuejs2 vue-resource

我试图在带有拦截器的请求标头中添加授权令牌,但它无法正常工作。我在拦截器内推的功能没有运行。

我在main.js中添加了以下代码,但函数内部的代码永远不会运行

Vue.http.interceptors.push(function (request, next) {
  console.log('Interceptors')
  console.log(request)
  request.headers.set('Authorization', 'Bearer eyJ..kPw')
  request.headers.set('Accept', 'application/json')
  next()
})

我记录了 Vue.http.interceptors 并显示了这个:

["before", "method", "body", "jsonp", "header", "cors"]

但是我无法弄清楚推送功能为什么不起作用。

这是我的main.js

import { sync } from 'vuex-router-sync'

import Vue from 'vue'
import Vuex from 'vuex'
import VueResource from 'vue-resource'
import VueRouter from 'vue-router'

Vue.use(Vuex)
Vue.use(VueResource)
Vue.use(VueRouter)

import App from './App'
import routes from './router'
import VuexStore from './vuex/store'

Vue.http.options.root = process.env.SERVER
console.log(Vue.http.interceptors)
Vue.http.interceptors.push(function (request, next) {
console.log('Interceptors')
console.log(request)
request.headers.set('Authorization', 'Bearer eyJ..kPw')
request.headers.set('Accept', 'application/json')
next()
})
Vue.config.productionTip = false

const store = new Vuex.Store(VuexStore)
const router = new VueRouter({
   routes
 })

sync(store, router)

/* eslint-disable no-new */
new Vue({
   el: '#app',
   router,
   template: '<App/>',
   components: { App }
})

0 个答案:

没有答案