$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>javascript - How to set an header with Axios module and vuejs in nuxt - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to set an header with Axios module and vuejs in nuxt - Stack Overflow

matteradmin12PV0评论

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.

Share Improve this question asked Jan 15, 2020 at 8:43 JeremyJeremy 1,9625 gold badges27 silver badges50 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Use $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;
    }
});

Post a comment

comment list (0)

  1. No comments so far