如何在vue-router中传递参数并重定向路由。

4

我想按照以下方式组织我的Vue Router:

原始URL 重定向到
exmple.com/ exmple.com/en/home
exmple.com/:lang exmple.com/:lang/home (:lang 是一个参数,表示语言)
example.com/:lang/home :lang 的主页
example.com/:lang/result/:date 日期的结果页面
example.com/:lang/result exmaple.com/:lang/result/current_date (current_date 可以被视为 new Date())

下面是我的vue-router配置

const router = new VueRouter({
    mode: 'history',
    routes: [{
        path: '/',
        redirect: '/en/home',
    },{
        path: '/:lang',
        name: 'lang',
        component: () => import("./Frame.vue"),
        redirect: {name: 'home'},
        children: [{
            path: 'home',
            name: 'home',
            component: () => import("./components/Home.vue")
        },{
            path: 'result/:date',
            name: 'result',
            component: () => import("./components/ResultDay.vue")
        },{
            path: 'result',
            redirect: {name: 'result', params: {date: new Date()}},
        }]
    }]
});

但是它无法从example.com/en/result重定向到example.com/en/result/current_date。JS控制台显示错误为

[vue-router]缺少命名路由“result”的参数:期望定义“lang”

那么我该如何将参数lang传递给“result”路由器?


1
如果你想要实现国际化的网站,可以尝试使用这个:https://kazupon.github.io/vue-i18n/ - cereme
1个回答

0
模式:'history', 路由:[{     路径:'/',      //重定向:' / en / home ',      //将默认设置为lang基本URL      重定向:() => {          返回'/en'      } },{     路径: '/:lang',     名称:'lang',     //组件:()=> import(“./Frame.vue”),     组件:{         模板:' ', //如果Frame.vue中没有特殊代码     },     //重定向:{name:'home'},     重定向:to => ({         路径: '/'+to.params.lang+'/home',//用户通过: lang传递的语言     }),     子项:[{         路径:'home',         名称:'home',         组件:()=> import("./components/Home.vue")     },{         路径:'result/:date',         名称:'result',         组件:()=> import("./components/ResultDay.vue")     },{         路径:'result',         重定向:{name:'result',params:{date:new Date()}},     }] }]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接