我有一个应用程序,用户可以登录在不同的角色,例如。seller,buyer和admin。对于每个用户,我想在相同的路径上显示仪表板页面,例如。但是,每个用户将在不同的vue组件中定义不同的仪表板,例如。SellerDashboard,BuyerDashboard和AdminDashboard。
因此,基本上,当用户打开http://localhost:8080/dashboard时,vue应用程序应该根据用户角色加载不同的组件(我将其存储在vuex中)。同样的,我也想买这个给其他航线。例如,当用户进入配置文件页面时,http://localhost:8080/profile应用程序应该根据登录用户的不同显示不同的配置文件组件。
因此,我希望对所有用户角色都有相同的路由,而不是对每个用户角色有不同的路由,例如。我不希望用户角色包含在url中,比如:http://localhost:8080/admin/profile和http://localhost:8080/seller/profile等等.
如何使用vue路由器实现此场景?
我尝试使用子路由和每个路由保护beforeEnter的组合来解析基于用户角色的路由。下面是一个代码示例:
在router.js
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import store from '@/store'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'home',
component: Home,
beforeEnter: (to, from, next) => {
next({ name: store.state.userRole })
},
children: [
{
path: '',
name: 'admin',
component: () => import('@/components/Admin/AdminDashboard')
},
{
path: '',
name: 'seller',
component: () => import('@/components/Seller/SellerDashboard')
},
{
path: '',
name: 'buyer',
component: () => import('@/components/Buyer/BuyerDashboard')
}
]
},
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes
})
export default router在store.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
userRole: 'seller' // can also be 'buyer' or 'admin'
}
})App.vue包含顶层路由的父路由器视图,例如。将/映射到Home组件,将/about映射到About组件:
<template>
<router-view/>
</template>
<script>
export default {
name: 'App',
}
</script>Home.vue包含针对不同用户的基于角色的组件的嵌套router-view:
<template>
<div class="home fill-height" style="background: #ddd;">
<h1>Home.vue</h1>
<!-- nested router-view where user specific component should be rendered -->
<router-view style="background: #eee" />
</div>
</template>
<script>
export default {
name: 'home'
}
</script>但是它不起作用,因为当我在Maximum call stack size exceeded中调用next({ name: store.state.userRole })时,会在浏览器控制台中获得beforeEnter异常。例外是:
vue-router.esm.js?8c4f:2079 RangeError: Maximum call stack size exceeded
at VueRouter.match (vue-router.esm.js?8c4f:2689)
at HTML5History.transitionTo (vue-router.esm.js?8c4f:2033)
at HTML5History.push (vue-router.esm.js?8c4f:2365)
at eval (vue-router.esm.js?8c4f:2135)
at beforeEnter (index.js?a18c:41)
at iterator (vue-router.esm.js?8c4f:2120)
at step (vue-router.esm.js?8c4f:1846)
at runQueue (vue-router.esm.js?8c4f:1854)
at HTML5History.confirmTransition (vue-router.esm.js?8c4f:2147)
at HTML5History.transitionTo (vue-router.esm.js?8c4f:2034)因此什么都没有呈现出来。
我有办法解决这个问题吗?
发布于 2019-12-19 10:46:17
您可能想尝试一下这个解决方案:
<template>
<component :is="compName">
</template>
data: () {
return {
role: 'seller' //insert role here - maybe on `created()` or wherever
}
},
components: {
seller: () => import('/components/seller'),
admin: () => import('/components/admin'),
buyer: () => import('/components/buyer'),
}或者如果你更喜欢整洁一点(同样的结果):
<template>
<component :is="loadComp">
</template>
data: () => ({compName: 'seller'}),
computed: {
loadComp () {
const compName = this.compName
return () => import(`/components/${compName}`)
}
}这将使您可以使用动态组件,而不必预先导入所有cmps,但每次只使用所需的组件。
发布于 2019-12-16 16:38:39
一种方法是使用动态分量。您可以有一个子路由,其组件也是非特定的(例如,DashboardComponent):
router.js
const routes = [
{
path: '/',
name: 'home',
children: [
{
path: '',
name: 'dashboard',
component: () => import('@/components/Dashboard')
}
]
}
]components/Dashboard.vue
<template>
<!-- wherever your component goes in the layout -->
<component :is="dashboardComponent"></component>
</template>
<script>
import AdminDashboard from '@/components/Admin/AdminDashboard'
import SellerDashboard from '@/components/Seller/SellerDashboard'
import BuyerDashboard from '@/components/Buyer/BuyerDashboard'
const RoleDashboardMapping = {
admin: AdminDashboard,
seller: SellerDashboard,
buyer: BuyerDashboard
}
export default {
data () {
return {
dashboardComponent: RoleDashboardMapping[this.$store.state.userRole]
}
}
}
</script>发布于 2019-12-25 13:45:08
此类代码仅检索给定角色的组件代码:
import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";
import store from "../store";
Vue.use(VueRouter);
const routes = [
{
path: "/",
name: "home",
component: () => {
switch (store.state.userRole) {
case "admin":
return import("../components/AdminDashboard");
case "buyer":
return import("../components/BuyerDashboard");
case "seller":
return import("../components/SellerDashboard");
default:
return Home;
}
}
}
];
const router = new VueRouter({
mode: "history",
base: process.env.BASE_URL,
routes
});
export default router;https://stackoverflow.com/questions/59360512
复制相似问题