gpt4 book ai didi

javascript - Vue路由器-如何根据用户 Angular 色在同一路由路径上加载多个组件?

转载 作者:行者123 更新时间:2023-12-04 15:38:23 34 4
gpt4 key购买 nike

我有应用程序,用户可以在其中以不同的 Angular 色登录,例如。 seller , buyeradmin .
对于每个用户,我想在同一路径上显示仪表板页面,例如。 http://localhost:8080/dashboard但是,每个用户将在不同的 vue 组件中定义不同的仪表板,例如。 SellerDashboard , BuyerDashboardAdminDashboard .

所以基本上,当用户打开 http://localhost:8080/dashboard vue 应用程序应该根据用户 Angular 色(我存储在 vuex 中)加载不同的组件。同样,我想将它用于其他路线。例如,当用户转到个人资料页面时 http://localhost:8080/profile应用程序应根据登录用户显示不同的配置文件组件。

所以我想为所有用户 Angular 色设置相同的路由,而不是为每个用户 Angular 色设置不同的路由,例如。我不希望用户 Angular 色包含在 url 中,如下所示:http://localhost:8080/admin/profilehttp://localhost:8080/seller/profile ETC...

如何使用 vue 路由器实现此场景?

我尝试使用子路由和每个路由守卫的组合 beforeEnter根据用户 Angular 色解析为路由。这是一个代码示例:

路由器.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'
}
})

应用程序.vue 包含顶级路由的父路由器 View ,例如。 map /Home组件和 /aboutAbout零件:

<template>
<router-view/>
</template>

<script>
export default {
name: 'App',
}
</script>

主页.vue 包含嵌套 router-view对于不同用户的基于 Angular 色的组件:

<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 .异常(exception)是:
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)

因此没有渲染任何内容。

有没有办法解决这个问题?

最佳答案

您可能想尝试解决此解决方案:

<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}`)
}
}

这将使您可以使用动态组件,而无需预先导入所有 cmp,而每次只使用所需的一个。

关于javascript - Vue路由器-如何根据用户 Angular 色在同一路由路径上加载多个组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59360512/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com