大家好,我有一个仪表板,其中包含所有组件的菜单:我希望当我以管理员角色登录时,我想显示所有组件,如果我以负责任的角色登录,我只想显示命令组件
我不知道该怎么做
这是我的 menu.ts import { MenuItem } from "./menu.model";
export const MENU: MenuItem[] = [
{
label: "Navigation",
isTitle: true,
},
{
label: "Dashboard",
icon: "home",
link: "/",
badge: {
variant: "success",
text: "1",
},
},
{
label: "Apps",
isTitle: true,
},
{
label: "Commandes",
icon: "box",
link: "/commandes",
},
{
label: "Clients",
icon: "users",
subItems: [
{
label: "Client professionelle",
icon: "user",
link: "/AgentPro",
},
{
label: "Client Particulier",
icon: "user",
link: "/clientpar",
},
],
},
{
label: "Responsable Depot",
icon: "eye",
link: "/ResponsableDepot",
},];
这是我的身份验证服务:
constructor(private http: HttpClient, private cookieService: CookieService) {}
/**
* Returns the current user
*/
public currentUser(): User {
if (!this.user) {
this.user = JSON.parse(this.cookieService.getCookie("currentUser"));
}
return this.user;
}
/**
* Performs the auth
* @param email email of user
* @param password password of user
*/
///api/login
//khasni njib dak refs hna
login(email: string, password: string) {
return this.http
.post<any>(` http://127.0.0.1:8000/api/login`, { email, password })
.pipe(
map((user) => {
// login successful if there s a jwt token in the response
if (user && user.token) {
this.user = user;
// store user details and jwt in cookie
this.cookieService.setCookie(
"currentUser",
JSON.stringify(user),
1
);
}
return user;
console.log("this is the user infos ", user);
})
);
}
/**
* Logout the user
*/
logout() {
// remove user from local storage to log user out
this.cookieService.deleteCookie("currentUser");
this.user = null;
}
我的后卫:
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const currentUser = this.authenticationService.currentUser();
if (currentUser) {
if (
route.data.roles &&
route.data.roles.indexOf(currentUser.roles) === -1
) {
// role not authorised so redirect to home page
this.router.navigate(["/"]);
return false;
}
// logged in so return true
return true;
}
// not logged in so redirect to login page with the return url
this.router.navigate(["/account/login"], {
queryParams: { returnUrl: state.url },
});
return false;
}
路由组件:
{
path: "",
component: LayoutComponent,
loadChildren: () =>
import("./pages/pages.module").then((m) => m.PagesModule),
canActivate: [AuthGuard],
},
{
path: "",
component: LayoutComponent,
loadChildren: () =>
import("./components/clients-par/client.module").then(
(m) => m.ClientModule
),
canActivate: [AuthGuard],
},
{
path: "",
component: LayoutComponent,
loadChildren: () =>
import("./components/clients-pro/clientpro.module").then(
(m) => m.ClientproModule
),
canActivate: [AuthGuard],
},
{
path: "",
component: LayoutComponent,
loadChildren: () =>
import("./components/commandes/commandes.module").then(
(m) => m.CommandesModule
),
canActivate: [AuthGuard],
},
这就是我的仪表板的样子: 图像