Abdullah Zafar
Abdullah Zafar

Reputation: 187

Access Nuxt Context in utils.js for using localePath nuxt-i18n

I want to use localePath in baseUrl but nuxt context is not available here. Is there any solution to make it possible. Thanks in Advance

  axios.create({
    baseURL: `${API_DOMAIN}/${localePath}/${apiURL}`,
    headers: requestHeaders(),
    paramsSerializer: (params) => Qs.stringify(params, { arrayFormat: 'brackets' }),
  });

export default client;

Upvotes: 0

Views: 878

Answers (1)

Abdullah Zafar
Abdullah Zafar

Reputation: 187

I fixed that one by checking if(!isServer)

let localePath = 'en'
const isServer = typeof window === "undefined"
if (!isServer) localePath = window.$nuxt.$store.$i18n.locale

axios.create({
 baseURL: `${API_DOMAIN}/${localePath}/${apiURL}`,
 headers: requestHeaders(),
 paramsSerializer: (params) => Qs.stringify(params, { arrayFormat: 'brackets' }),
});

export default client;

Upvotes: 0

Related Questions