当我想传递参数时,路由器推送在 vuejs 中不起作用

router push doesn't work in vuejs when i want to pass params

当我想通过 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 },

您的路由定义不接受参数。你应该定义你的路线如下:

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

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

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

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

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