Vue2 / Vue Router:设置来自导航守卫的数据

时间:2021-05-27 06:58:40

标签: vuejs2 vue-router

这应该相当简单,但我无法弄清楚。我想从 vue 路由器的导航守卫中设置一个值。我什至不再关心这个值的去向,只是某处。可能在 App 根目录上,也可能在组件上。随处。
到目前为止,我已经尝试过:

router.beforeResolve((to, from, next) => {

// this throws Warning "Method "setValue" has type "boolean" in the component definition. Did you reference the function correctly?"
  MyComponent.methods.setValue(true);

// this works, but only here
  next(vm=>{ vm.value=true });
});

router.afterEach((to, from) => {
  // no vm. Same error on component method.
});

我真的很想避免使用 Vuex,因为(到目前为止)它只是我想传递的一个微弱的值。有什么想法吗?

0 个答案:

没有答案