如何添加用户和管理员导航在角度上分开



我想像电子商务网站一样,在angular项目中分离管理和用户导航。但儿童路线不适用于仪表板或用户。当我点击仪表板内的报告路由时,它会出错。这是我的代码

应用程序/应用程序路由模块.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { DashboardComponent } from './admin/dashboard.component';
import { HomeComponent } from './components/home/home.component';

const routes: Routes = [
{path: '', component: HomeComponent},
{path: 'dashboard', component: DashboardComponent}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }

app/app.component.html

<router-outlet></router-outlet>

管理员/管理模块.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AdminRoutingModule } from './admin-routing.module';
import { OrderComponent } from './components/order/order.component';
import { ProductComponent } from './components/product/product.component';
import { CustomerComponent } from './components/customer/customer.component';
import { ReportComponent } from './components/report/report.component';

@NgModule({
declarations: [OrderComponent, ProductComponent, CustomerComponent, ReportComponent],
imports: [
CommonModule,
AdminRoutingModule
]
})
export class AdminModule { }

管理/管理路由模块.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { ReportComponent } from './components/report/report.component';

const routes: Routes = [
{path: 'reports', component: ReportComponent}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class AdminRoutingModule { }

管理员/仪表板组件.html

<h1>Dashboard</h1>
<li class="nav-item">
<a class="nav-link" [routerLink]="['/reports']" routerLinkActive="router-link-active" >
<span data-feather="bar-chart-2"></span>
Reports
</a>
</li>
<router-outlet></router-outlet>

Admin/components/report/report.components.html

<p>report works!</p>

但当我试图访问这条路线时,它会出现错误

错误:未捕获(承诺中(:错误:无法匹配任何路由。URL段:"reports">

我认为如果您在AppRoutingModule中使用路由的children属性会更清楚,如下所示:

我使用了布局组件,但您可以使用您的模块。

我实施了我的一个项目。你可以在我的github 上看看

const routes: Routes = [
{
path: "login", component: AuthlayoutComponent, children: [
{ path: "", component: LoginComponent }
]
},
{
path: "admin", component: AdminlayoutComponent, children: [
{ path: 'home', component: HomeComponent, pathMatch: 'full', canActivate: [AuthGuard] },
{ path: 'report', component: ReportComponent, canActivate: [AuthGuard] }
]
},
{
path: "user", component: UserlayoutComponent, children: [
{ path: 'home', component: UserHomeComponent, pathMatch: 'full', canActivate: [AuthGuard] },
{ path: 'report', component: UserReportComponent, canActivate: [AuthGuard] }
]
},
{
path: '**', component: AuthlayoutComponent, children: [
{ path: "", component: LoginComponent }
]
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }

相关内容

  • 没有找到相关文章

最新更新