VUE2-根据商店值更改路线

发布于 2025-01-23 02:57:38 字数 1107 浏览 0 评论 0原文

我正在尝试根据Vuex商店中保存的值更改组件的路由。有四个不同的模板文件,保存在不同的目录中(Template1,Template2等)。我正在从详细信息存储中提取模板ID,并取决于此值,希望从正确的目录呈现组件。

例如,如果商店中的模板值为2,则加载所有template2/profile组件,如果是三个加载template3/profile component。

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


Vue.use(VueRouter)

const setComponent = componentName => {
    return () => import(`../template${store.getters['detailsStore/getTemplate']}/views/${componentName}`)
}

const routes = [
    {
        path: '/',
        redirect: '/details'
    },
    {
        path: '/profile',
        name: 'Profile',
        //component: () => import('../template2/views/Profile') // - this is how i was importing the component but couldn't access vuex store
        component: () => setComponent('Profile'),
    },
    {
        path: '/details',
        name: 'Details',
        component: () => setComponent('Details'),
    }
];

const router = new VueRouter({
  mode: 'history',
  routes
})

export default router

我认为创建setComponent函数会有所帮助。但是我只有一个空白页。任何帮助将不胜感激

I am trying to change the route of my components based on a value saved within the vuex store. There are four different template files, saved within different directories (template1, template2 etc). I am pulling the template id from the details store and depending on this value would like to render the component from the correct directory.

For example, if the template value in the store is 2 then load all the template2/profile component, if it is three load the template3/profile component.

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


Vue.use(VueRouter)

const setComponent = componentName => {
    return () => import(`../template${store.getters['detailsStore/getTemplate']}/views/${componentName}`)
}

const routes = [
    {
        path: '/',
        redirect: '/details'
    },
    {
        path: '/profile',
        name: 'Profile',
        //component: () => import('../template2/views/Profile') // - this is how i was importing the component but couldn't access vuex store
        component: () => setComponent('Profile'),
    },
    {
        path: '/details',
        name: 'Details',
        component: () => setComponent('Details'),
    }
];

const router = new VueRouter({
  mode: 'history',
  routes
})

export default router

I thought creating the setComponent function would help,. but i just get a blank page. Any help would be much appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

删除→记忆 2025-01-30 02:57:38

VUE2:

您的组件未完成加载。这将做到这一点:

const asyncComp = async (componentName) => {
  const component = await import(`../template${store.getters['detailsStore/getTemplate']}/views/${componentName}`)
  return component;
};


{
  path: '/profile',
  name: 'Profile',
  component: () => asyncComp('Profile'),
}

VUE 3解决方案:
在这里,您将拥有可用的DefereSyncomponent()
https://vuejs.org/api/papi/gapi/general.html.html #html#defneasyneasyneasyneasyneasynccomponent

const asyncComp = (compName) => defineAsyncComponent(() => import(`../template${store.getters['detailsStore/getTemplate']}/views/${compName}`);

Vue2:

Your component is not done loading. This will do it:

const asyncComp = async (componentName) => {
  const component = await import(`../template${store.getters['detailsStore/getTemplate']}/views/${componentName}`)
  return component;
};


{
  path: '/profile',
  name: 'Profile',
  component: () => asyncComp('Profile'),
}

Vue 3 solution:
Here you will have defineAsyncComponent() available
https://vuejs.org/api/general.html#defineasynccomponent

const asyncComp = (compName) => defineAsyncComponent(() => import(`../template${store.getters['detailsStore/getTemplate']}/views/${compName}`);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文