访问utils.js中的Nuxt上下文以使用localePath Nuxt-i18n



我想在baseUrl中使用localePath,但这里没有nuxt上下文。有什么解决方案可以使之成为可能吗。提前感谢

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

我通过检查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;

最新更新