VUE2-根据商店值更改路线
我正在尝试根据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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
VUE2:
您的组件未完成加载。这将做到这一点:
VUE 3解决方案:
在这里,您将拥有可用的DefereSyncomponent()
https://vuejs.org/api/papi/gapi/general.html.html #html#defneasyneasyneasyneasyneasynccomponent
Vue2:
Your component is not done loading. This will do it:
Vue 3 solution:
Here you will have defineAsyncComponent() available
https://vuejs.org/api/general.html#defineasynccomponent