【发布时间】:2018-12-06 12:30:12
【问题描述】:
我正在使用带有 VueJS 和 TypeScript 的支架式 AspNetCore 2.1 站点。 我正在尝试将kazupon i18n 插件与路由器视图集成。如果没有 URL 集成,它就可以正常工作。
我无法像http://localhost/en/product 和http://localhost/fr/product 那样进行正确的重定向
这是最初的boot.ts,它使用VueI18n
import Vue from 'vue';
import VueRouter from 'vue-router';
import VueI18n from 'vue-i18n'
Vue.use(VueRouter);
Vue.use(VueI18n);
import { messages, defaultLocale } from './lang/i18n';
const i18n = new VueI18n({
locale: defaultLocale,
fallbackLocale: 'en',
messages
})
const routes = [
{ path: '/', component: require('./components/home/home.vue.html') },
{ path: '/product', component: require('./components/product/product.vue.html') },
];
const router = new VueRouter({
mode: 'history',
routes: routes
});
new Vue({
el: '#app-root',
router: router,
i18n: i18n,
render: h => h(require('./components/app/app.vue.html'))
});
到目前为止,我已经尝试为 routes 添加前缀,但它只是破坏了功能:
const routes = [{
path: '/',
redirect: `/${defaultLocale}`,
},
{
path: '/:locale',
children: [
{ path: '/', component: require('./components/home/home.vue.html') },
{ path: '/counter', component: require('./components/product/product.vue.html') },
]
}];
我也尝试过依靠router.beforeEach 来设置语言环境。
router.beforeEach((to, from, next) => {
let language = to.params.locale;
if (!language) {
language = 'en';
}
i18n.locale = language;
next();
});
这也不起作用。
我从github.com/ashour/vuejs-i18n-demo 和vue-i18n-sap-multilingual-best-practice 中汲取了灵感,但似乎在 i18n 迁移期间,一些示例可能已经过时或丢失,并且这些用例不再起作用。
【问题讨论】:
-
请注意,在带有路径的示例中:
'/:locale'您缺少component: { template: '<router-view />', },元素。我的意思是像here。
标签: vue.js internationalization vuejs2 vue-router