简体   繁体   English

如何在 vue-routers 中使用参数重定向到路由器

[英]How to redirect to a router with params in vue-routers

I want to organize my vue router like this: (xxx.com is my domain)我想像这样组织我的 vue 路由器:(xxx.com 是我的域)

xxx.com/ -> redirect to xxx.com/en/home xxx.com/ -> 重定向到 xxx.com/en/home

xxx.com/:lang -> redirect to xxx.com/:lang/home (:lang is a param meaning language) xxx.com/:lang -> 重定向到 xxx.com/:lang/home (:lang 是一个参数,意思是语言)

xxx.com/:lang/home -> home page of:lang xxx.com/:lang/home -> 主页:lang

xxx.com/:lang/result/:date -> result page of a date xxx.com/:lang/result/:date -> 日期的结果页面

xxx.com/:lang/result -> redirect to xxx.com/:lang/result/current_date (current_date can be regarded as new Date()) xxx.com/:lang/result -> 重定向到 xxx.com/:lang/result/current_date (current_date 可视为 new Date())

Below is my vue-router config下面是我的 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()}},
        }]
    }]
});

But it cannot redirect from xxx.com/en/result to xxx.com/en/result/current_date.但它不能从 xxx.com/en/result 重定向到 xxx.com/en/result/current_date。 JS console shows an error as "[vue-router] missing param for named route "result": Expected "lang" to be defined" JS 控制台显示错误为“[vue-router] 缺少命名路由“结果”的参数:应定义“语言”

So how can I pass the param lang to the "result" router?那么如何将参数 lang 传递给“结果”路由器?

mode: 'history',
    routes: [{
        path: '/',
       //  redirect: '/en/home',
       // set default to lang base url 
        redirect: () => {
            return '/en'
        }
    },{
        path: '/:lang',
        name: 'lang',
        // component: () => import("./Frame.vue"),
        component: {
            template: '',  // if you have no special codes in Frame.vue
        },
        // redirect: {name: 'home'},
        redirect: to => ({
            path: '/'+to.params.lang+'/home',  // user lang passed by :lang
        }),
        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()}},
        }]
    }]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM