简体   繁体   English

Vue.js 中的条件路由

[英]Conditional route in Vue.js

I have two different views that I'd like to show for the same path depending on whether a token is in LocalStorage or not.我有两个不同的视图,我想根据令牌是否在 LocalStorage 中来显示相同​​的路径。 I could move the logic directly into the view itself, but I was curious to know whether there's a way to it in the Router.我可以将逻辑直接移到视图本身中,但我很想知道路由器中是否有实现它的方法。

Something like:就像是:

export default new Router({
  mode: "history",
  base: process.env.BASE_URL,
  routes: [
    {
      path: "/",
      name: "home",
      component: function() {
        if (...) {
          return ViewA
        } else {
          return ViewB
        }
      }
    },
  ]
});

I tried with the above code but didn't work.我试过上面的代码,但没有用。 The app builds fine but none of the two views is shown.该应用程序构建良好,但没有显示两个视图。

A getter could be used for this, but, you will need to be sure to import both components:可以为此使用 getter,但是,您需要确保导入这两个组件:

import ViewA from '@/views/ViewA'
import ViewB from '@/views/ViewB'

export default new Router({
    mode: "history",
    base: process.env.BASE_URL,
    routes: [
        {
            path: "/",
            name: "home",
            get component() {
                if (...) {
                    return ViewA;
                } else {
                    return ViewB;
                }
            }
        },
    ]
});

In my notes, I have written "cannot find documentation on this" pertaining to the above.在我的笔记中,我写了与上述内容有关的“无法找到有关此的文档”。 While not specifically related, however, it might be helpful to review using some information from https://stackoverflow.com/a/50137354/3261560 regarding the render function.然而,虽然没有特别相关,但使用https://stackoverflow.com/a/50137354/3261560 中有关渲染功能的一些信息进行审查可能会有所帮助。 I've altered what is discussed there using your example above.我已经使用上面的示例更改了那里讨论的内容。

component: {
    render(c) {
        if (...) {
            return c(ViewA);
        } else {
            return c(ViewB);
        }
    }
}

I was answering the same question before and you can see it here .我之前回答过同样的问题,你可以在这里看到。

Here is an example:下面是一个例子:

routes: [
    {
      path: '*',
      beforeEnter(to, from, next) {
        let components = {
          default: [A, B, C][Math.floor(Math.random() * 100) % 3],
        };
        to.matched[0].components = components;

        next();
      }
    },

... where A, B, C are components and they are randomly chosen every time the route changes. ... 其中 A、B、C 是组件,每次路由更改时都会随机选择它们。 So in your case you can just alter beforeEnter logic to your needs and set any component you wish before routing to it.因此,在您的情况下,您可以根据需要更改beforeEnter逻辑,并在路由到它之前设置您希望的任何组件。

Alright so I found a simple way to do this using the webpack lazy loading functionality in vue router with vuex store state property;好的,所以我找到了一种简单的方法来使用 vue 路由器中的 webpack 延迟加载功能和 vuex 存储状态属性;

{
  path: '/',
  component: () => { 
    if (store.state.domain) {
      return import(/* webpackChunkName: "app-home" */ '../views/AppHome.vue');
    } else {
      return import(/* webpackChunkName: "home" */ '../views/Home.vue');
    }
  }
},

With the above code, my home component is dynamically imported and used the route component depending on the value of domain property in my vuex store.使用上面的代码,我的 home 组件被动态导入并根据我的 vuex 商店中domain属性的值使用路由组件。 Please note that you have to set up vuex store and import it in your router for this to work.请注意,您必须设置 vuex 商店并将其导入您的路由器才能正常工作。

The solution in the question would have worked if you returned the component as an import.如果您将组件作为导入返回,则问题中的解决方案会起作用。

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

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