admin管理员组文章数量:1026989
I'm trying to define an "Accept-Language" header for my SPA in vue.js (with nuxt).
Here's what I tried but it doesn't work. I specify that I use the axios module for nuxt.
I created a plugin as explained in the documentation. I included the plugin in nuxt.config.js.
I tried to use setHeader
as explained here, but it doesn't work.
export default function ({ store, $axios, redirect }) {
$axios.setBaseURL(process.env.BASE_URL);
if (process.server) {
return
}
$axios.onRequest(config => {
const baseUrl = $axios.defaults.baseURL;
const locale = store.getters['lang/locale'];
if (locale) {
$axios.setHeader('Accept-Language', locale)
}
});
}
But this code doesn't work. However, when I make console.log
, I see them so it is taken into account.
I'm trying to define an "Accept-Language" header for my SPA in vue.js (with nuxt).
Here's what I tried but it doesn't work. I specify that I use the axios module for nuxt.
I created a plugin as explained in the documentation. I included the plugin in nuxt.config.js.
I tried to use setHeader
as explained here, but it doesn't work.
export default function ({ store, $axios, redirect }) {
$axios.setBaseURL(process.env.BASE_URL);
if (process.server) {
return
}
$axios.onRequest(config => {
const baseUrl = $axios.defaults.baseURL;
const locale = store.getters['lang/locale'];
if (locale) {
$axios.setHeader('Accept-Language', locale)
}
});
}
But this code doesn't work. However, when I make console.log
, I see them so it is taken into account.
2 Answers
Reset to default 3Use $axios.interceptors instead of $axios.onRequest
export default function({store, $axios, redirect}) {
$axios.setBaseURL(process.env.BASE_URL);
if (process.server) {
return
}
$axios.interceptors.request.use((config) => {
const baseUrl = $axios.defaults.baseURL;
const locale = store.getters['lang/locale'];
if (locale) {
$axios.setHeader('Accept-Language', locale)
}
return config;
});
}
how are you making the request? If you want to use the helpers (setHeader, setToken, ...) provided by @nuxtjs/axios, you then must always use the request helpers provided by it, that are simply the lowercase http method names prefixed with a $
Instead of $axios.get( ... )
use $axios.$get( ... )
and so on for put, post, delete...
You can also try setting headers this way:
$axios.onRequest(config => {
const baseUrl = $axios.defaults.baseURL;
const locale = store.getters['lang/locale'];
if (locale) {
config.headers.mon['Accept-Language'] = locale;
}
});
I'm trying to define an "Accept-Language" header for my SPA in vue.js (with nuxt).
Here's what I tried but it doesn't work. I specify that I use the axios module for nuxt.
I created a plugin as explained in the documentation. I included the plugin in nuxt.config.js.
I tried to use setHeader
as explained here, but it doesn't work.
export default function ({ store, $axios, redirect }) {
$axios.setBaseURL(process.env.BASE_URL);
if (process.server) {
return
}
$axios.onRequest(config => {
const baseUrl = $axios.defaults.baseURL;
const locale = store.getters['lang/locale'];
if (locale) {
$axios.setHeader('Accept-Language', locale)
}
});
}
But this code doesn't work. However, when I make console.log
, I see them so it is taken into account.
I'm trying to define an "Accept-Language" header for my SPA in vue.js (with nuxt).
Here's what I tried but it doesn't work. I specify that I use the axios module for nuxt.
I created a plugin as explained in the documentation. I included the plugin in nuxt.config.js.
I tried to use setHeader
as explained here, but it doesn't work.
export default function ({ store, $axios, redirect }) {
$axios.setBaseURL(process.env.BASE_URL);
if (process.server) {
return
}
$axios.onRequest(config => {
const baseUrl = $axios.defaults.baseURL;
const locale = store.getters['lang/locale'];
if (locale) {
$axios.setHeader('Accept-Language', locale)
}
});
}
But this code doesn't work. However, when I make console.log
, I see them so it is taken into account.
2 Answers
Reset to default 3Use $axios.interceptors instead of $axios.onRequest
export default function({store, $axios, redirect}) {
$axios.setBaseURL(process.env.BASE_URL);
if (process.server) {
return
}
$axios.interceptors.request.use((config) => {
const baseUrl = $axios.defaults.baseURL;
const locale = store.getters['lang/locale'];
if (locale) {
$axios.setHeader('Accept-Language', locale)
}
return config;
});
}
how are you making the request? If you want to use the helpers (setHeader, setToken, ...) provided by @nuxtjs/axios, you then must always use the request helpers provided by it, that are simply the lowercase http method names prefixed with a $
Instead of $axios.get( ... )
use $axios.$get( ... )
and so on for put, post, delete...
You can also try setting headers this way:
$axios.onRequest(config => {
const baseUrl = $axios.defaults.baseURL;
const locale = store.getters['lang/locale'];
if (locale) {
config.headers.mon['Accept-Language'] = locale;
}
});
本文标签: javascriptHow to set an header with Axios module and vuejs in nuxtStack Overflow
版权声明:本文标题:javascript - How to set an header with Axios module and vuejs in nuxt - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745662685a2161987.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论