如何在 vue-cli 中设置 HTTP header?

How to set HTTP header in vue-cli?

我想在 vue-cli 中设置自定义 header。

vue版本为2.5.2.

我试过 main.js:

import Vue from 'vue'
import App from './App'
import router from './router'

Vue.config.productionTip = false
Vue.http.headers.common['Cache-Control'] = 'no-cache, no-store, must-revalidate'

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

但是 returns:

Uncaught TypeError: Cannot read property 'headers' of undefined

我认为您需要安装 vue-resource 或 vue-axios 库才能使其工作并进行 REST 调用。

请安装并重试。

https://github.com/pagekit/vue-resource

https://github.com/imcvampire/vue-axios

希望对您有所帮助。

您可以使用 vue.config.jsdevServer 选项,它只是将它代理到 webpack

设置headers:

devServer: {
   headers: { "Cache-Control": "no-cache, no-store, must-revalidate" }
}