我有一个多using应用程序使用nuxt-i18n与被忽略的路线。
nuxt.config.js
…
seo: true,
parsePages: false,
strategy: 'prefix_except_default',
pages: {
'cats': {
en: '/cats',
de: '/katzen',
fr: false
}
}
…因此,该页面无法以法语提供。
我的lang开关看起来像这样-到目前为止很简单:
LanguageSwitch.vue
computed: {
availableLocales () {
return this.$i18n.locales.filter(i => i.code !== this.$i18n.locale)
}
}<ul class="language-switch__list">
<li class="language-switch__item" v-for="locale in availableLocales">
<NuxtLink
class="language-switch__link"
rel="alternate" :key="locale.code"
:to="switchLocalePath(locale.code)"
:hreflang="locale.code" :lang="locale.code"
active-class="none"
exact>
{{locale.name}}
</NuxtLink>
</li>
</ul>我已经更改了过滤器以删除缺少的页面/语言,如:
return this.$i18n.locales.filter(i => (i.code !== this.$i18n.locale) && (this.$nuxt.$route.path !== this.switchLocalePath(i.code)) )
这很管用,但我想要一个更好的解决方案。下面是我的问题:如果忽略路由,是否有一种简单的方法将路由更改为语言主页(/、/en、/de)?
发布于 2020-02-21 13:31:06
我解决了这个问题,简单地在它周围包装了一个额外的函数:
<nav id="language-switch" v-if="isOpen" class="language-switch__nav arrow arrow--top">
<ul class="language-switch__list">
<li class="language-switch__item" v-for="locale in availableLocales">
<NuxtLink class="language-switch__link" rel="alternate" :key="locale.code" :to="switchLocalePathFallback(locale.code)" :hreflang="locale.code" :lang="locale.code" active-class="none" exact>{{locale.name}}</NuxtLink>
</li>
</ul>
</nav>…
computed: {
availableLocales () {
return this.$i18n.locales.filter(i => (i.code !== this.$i18n.locale) )
}
},
methods: {
switchLocalePathFallback(code) {
let langPath = this.switchLocalePath(code),
path = langPath
if(langPath == this.$nuxt.$route.path ) {
path = this.$i18n.defaultLocale != code ? '/'+code : '/'
}
return path
}
}
…不太灵活,但对我来说很管用。
https://stackoverflow.com/questions/60285308
复制相似问题