如何在解析 vue 中的路由之前发送新参数?

How to send new params before resolving a route in vue?

我的这条路线有一个 value 参数

  routes.push({
    name: 'test',
    path: '/test/:value',
    component: resolve(__dirname, 'src/pages/test.vue'),
  });

我想更新路由,以便它实际发送 valuetest 作为参数

  routes.push({
    name: 'test',
    path: '/test/:value',
    component: resolve(__dirname, 'src/pages/test.vue'),
    beforeEnter(to, from, next) {
      to.params.test = 'test';

      next({ params: to.params });
    },
  });

然后我可以在一个vue页面中捕捉它

async fetch({ store, route }) {
  console.log(route.params.value);
  console.log(route.params.test);
},

我在上面创建的 beforeEnter 代码不起作用。新的 test 参数没有被发送。

有什么想法吗?

尝试为下一个提供 path/name,如下所示:

  routes.push({
    name: 'test',
    path: '/test/:value',
    component: resolve(__dirname, 'src/pages/test.vue'),
    beforeEnter(to, from, next) {
      next({ name: 'test', params: { test: 'test' });
    },
  });

显然 next 将 params 对象作为参数

next(to.params)

有效,但我在文档中找不到它

编辑Working example

您确定问题不在其他地方吗?尝试按照示例 codepen

中的方式记录参数