mls*_*lst 14 javascript vue.js vue-router vuex
我有应用程序,用户可以在其中以不同的角色登录,例如。seller
,buyer
和admin
。对于每个用户,我想在同一路径上显示仪表板页面,例如。http://localhost:8080/dashboard
但是,每个用户都会在不同的 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
Run Code Online (Sandbox Code Playgroud)
在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'
}
})
Run Code Online (Sandbox Code Playgroud)
App.vue包含顶级路由的父路由器视图,例如。映射/
到Home
分量和/about
以About
组分:
<template>
<router-view/>
</template>
<script>
export default {
name: 'App',
}
</script>
Run Code Online (Sandbox Code Playgroud)
并且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>
Run Code Online (Sandbox Code Playgroud)
但是,因为我得到它不工作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)
Run Code Online (Sandbox Code Playgroud)
因此没有渲染任何内容。
有没有办法解决这个问题?
您可能想尝试围绕此解决方案进行一些操作:
<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'),
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您更喜欢整洁一点(结果相同):
<template>
<component :is="loadComp">
</template>
data: () => ({compName: 'seller'}),
computed: {
loadComp () {
const compName = this.compName
return () => import(`/components/${compName}`)
}
}
Run Code Online (Sandbox Code Playgroud)
这将使您可以使用动态组件,而不必预先导入所有 cmps,而每次只使用需要的一个。
一种方法是使用动态组件。您可以有一个子路由,其组件也是非特定的(例如DashboardComponent
):
路由器.js
const routes = [
{
path: '/',
name: 'home',
children: [
{
path: '',
name: 'dashboard',
component: () => import('@/components/Dashboard')
}
]
}
]
Run Code Online (Sandbox Code Playgroud)
组件/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>
Run Code Online (Sandbox Code Playgroud)
此类代码仅检索给定角色的组件代码:
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;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9064 次 |
最近记录: |