首页 > 解决方案 > 当我想传递参数时,路由器推送在 vuejs 中不起作用

问题描述

当我想通过vue-router包使用编程导航时,它可以工作,但是当我想将参数传递给带有router.push方法的组件时,它根本不起作用。有人有解决方案吗?

我的代码在这里:

import VueRouter from 'vue-router'
import routes from './routes';

const router = new VueRouter({routes});

Vue.use(VueRouter);

和推送代码:

router.push({ name: 'reportArchive', params: {token: token} });

我的路线配置:

{ path: '/reportArchive', name: 'reportArchive', component: reportArchive },

标签: javascriptvue.jsvuejs2vue-componentrouter

解决方案


如果你真的想传递参数,你需要设置接受参数的路由,如下所示:

{ path: '/reportArchive/:token', name: 'reportArchive', component: reportArchive },

这是按照Eldar上面的回答,但是如果你想传递url查询参数,你需要在代码中使用query而不是params,例如:

router.push({ name: 'reportArchive', query: {token: token} });

推荐阅读