![](/img/trans.png)
[英]How to get domain in middleware, then pass it to components/store before axios calls [Nuxt]
[英]How to get current domain at Nuxt 3 middleware?
我不能在這里使用 window.location 因為它是 SSR 應用程序。 useRouter、useRoute 和 useNuxtApp 也沒有域名。 nuxtApp.ssrContext 未定義。
export default defineNuxtRouteMiddleware((to: any, from: any) => {
console.log("GET HOST HERE")
})
我只是錯過了在獲取 nuxtApp.ssrContext 之前檢查 process.server。 這是我的問題的答案:
export default defineNuxtRouteMiddleware((to: any, from: any) => {
const nuxtApp = useNuxtApp()
let host = ''
if(process.server) {
host = nuxtApp.ssrContext.req.headers.host
} else {
host = window.location.host
}
})
發布后我使用
useNuxtApp().ssrContext.event.node.req.headers.host
代替
useNuxtApp().ssrContext.req.headers.host
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.