English 中文(简体)
Nuxt i18n在中间件中调用useRoute可能会导致误导性结果
原标题:Nuxt i18n Calling useRoute within middleware may lead to misleading results

So I am getting this warning in nuxt3: Calling useRoute within middleware may lead to misleading results. Instead, use the (to, from) arguments passed to the middleware to access the new and old routes.

发生这种情况是因为我在中间件中调用<code>useLocalePath()</code>。

这是发生这种情况的中间件之一:

export default defineNuxtRouteMiddleware(async(to, from) => {
    const localPath = useLocalePath()

    const isUserAuthenticated = await isAuthenticated()

    if (isUserAuthenticated) {
        if (to.fullPath === localPath( login ) || to.fullPath === localPath( register )) {
            return navigateTo(localPath( / ))
        }
    } else {
        if (to.fullPath !== localPath( login ) && to.fullPath !== localPath( register )) {
            return navigateTo(localPath( login ))
        }
    }

})

我的nuxt.config.ts中有这个:

i18n: {
        lazy: true,
        langDir: "locales",
        strategy: "prefix_and_default",
        locales: [
            {
                code:  nl-Nl ,
                iso:  nl-Nl ,
                name:  Dutch ,
                file:  nl-NL.json 
            },
            {
                code:  en ,
                iso:  en ,
                name:  English ,
                file:  en.json 
            },
        ],
        detectBrowserLanguage: {
            useCookie: true,
            cookieCrossOrigin: true,
            alwaysRedirect: true,
            cookieKey:  i18n_redirected ,
            redirectOn:  root 
        },
        defaultLocale: "nl-Nl",
        customRoutes:  config ,
        pages: {
            pricing: {
                en:  /pricing ,
                 nl-Nl :  /prijzen ,
            }
        }
    }

This is the version of i18n that i m using: "@nuxtjs/i18n": "^8.0.0-beta.12",

问题是,代码运行得很好,但我不知道它为什么会给我这个警告。

忽视这个警告安全吗?

问题回答

useLocalePath()在引擎盖下使用useRoute()方法,这就是您收到警告的原因。一种解决方案可能是使用useI18n()并访问应用程序区域设置变量。

const { locale } = useI18n();

if (!to.fullPath.includes("login")) {
  return navigateTo(`${locale.value}/login`);
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签