我为Nuxt定义了一个名称空间中间件,如下所示:
export default function({ store, redirect, app }){
if (!store.state.isAuth) {
return redirect(app.i18n.localePath('/auth'))
}
}
如果用户未经过身份验证,则将其重定向到位于/auth
的身份验证页面。但是,这是一个多语言站点,因此我需要生成与当前地区相对应的路径。通常,我会使用$i18n.localePath
或者在本例中使用Nuxt的上下文,app.i18n.localePath()
来完成这个操作,但是我得到了这个错误:
app.i18n.localePath is not a function
我不知道为什么,考虑到有一个上下文,console.log(app.i18n)
向我展示了app.i18n.localePath是一个函数:
...
localePath: [Function: bound ],
...
有什么建议吗?谢谢!
发布于 2021-03-13 12:13:26
是的,由于某种原因,localePath不能在中间件中工作。您可能应该手动检测当前的区域设置。
let locale = app.i18n.locale === app.i18n.defaultLocale ? '' : '/' + app.i18n.locale;
return redirect( locale + '/auth' );
发布于 2021-09-28 09:40:12
使用app.localePath('/auth')
代替
https://stackoverflow.com/questions/66609106
复制相似问题