ng g component modules/components/plan
ng g module modules/components/plan --routing
ng generate module app-routing --flat --module=app
import {
NgModule } from '@angular/core';
import {
CommonModule } from '@angular/common';
@NgModule({
imports: [
CommonModule
],
declarations: []
})
export class AppRoutingModule {
}
import {
NgModule } from '@angular/core';
import {
RouterModule, Routes } from '@angular/router';
@NgModule({
exports: [ RouterModule ]
})
export class AppRoutingModule {
}
import {
NgModule } from '@angular/core';
import {
CommonModule } from '@angular/common';
import {
RouterModule, Routes } from '@angular/router';
import {
AutherticationGuardService } from './core/services/authertication-guard.service';
const routes: Routes = [
{
path: 'login',
loadChildren: './modules/login/login.module#LoginModule'
},
{
path: 'plan',
loadChildren: './modules/plan/plan.module#PlanModule',
canLoad: [AutherticationGuardService],
canActivate: [AutherticationGuardService],
canActivateChild: [AutherticationGuardService],
canDeactivate: [AutherticationGuardService],
},
{
path: 'design',
loadChildren: './modules/design/design.module#DesignModule'
},
{
path: 'execution',
loadChildren: './modules/execution/execution.module#ExecutionModule'
},
{
path: '',
pathMatch: 'full',
redirectTo: 'plan/plan-home/procedure-list',
}
];
plan-routing.module.ts
```javascript
const routes: Routes = [
{
path: 'plan-home',
component: PlanComponent,
children: [
{
path: 'procedure-list',
loadChildren: './modules/procedure-list/procedure-list.module#ProcedureListModule',
},
{
path: 'procedure-details',
loadChildren: './modules/procedure-details/procedure-details.module#ProcedureDetailsModule',
},
{
path: 'entity-list',
loadChildren: './modules/entity-list/entity-list.module#EntityListModule',
},
{
path: 'policy-coverage',
loadChildren: './modules/policy-coverage/policy-coverage.module#PolicyCoverageModule',
},
{
path: 'regulatory-coverage',
loadChildren: './modules/regulatory-coverage/regulatory-coverage.module#RegulatoryCoverageModule',
}
]
},
{
path: '',
pathMatch: 'full',
redirectTo: 'plan-home/procedure-list'
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class PlanRoutingModule {
}
procedure-details-routing.module.ts
import {
NgModule } from '@angular/core';
import {
Routes, RouterModule } from '@angular/router';
import {
ProcedureDetailsComponent } from './procedure-details.component';
const routes: Routes = [
{
path: 'details-home',
component: ProcedureDetailsComponent,
children: [
{
path: 'plan',
// loadChildren: './modules/details-plan/details-plan.module#DetailsPlanModule',
loadChildren: () => import('./modules/details-plan/details-plan.module').then(m => m.DetailsPlanModule),
},
{
path: 'design',
// loadChildren: './modules/details-design/details-design.module#DetailsDesignModule',
loadChildren: () => import('./modules/details-design/details-design.module').then(m => m.DetailsDesignModule),
},
{
path: 'execution',
// loadChildren: './modules/details-execution/details-execution.module#DetailsExecutionModule',
loadChildren: () => import('./modules/details-execution/details-execution.module').then(m => m.DetailsExecutionModule),
}
]
},
{
path: '',
pathMatch: 'full',
redirectTo: 'details-home/plan'
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class ProcedureDetailsRoutingModule {
}
@NgModule({
declarations: [],
imports: [
RouterModule.forRoot(routes, {
useHash: true })
],
exports: [RouterModule],
providers: [AutherticationGuardService]
})
export class AppRoutingModule {
}
注:查看app.module.ts文件中是否有引入AppRoutingModule。
import {
AppRoutingModule } from './app-routing/app-routing.module';
imports: [
AppRoutingModule
],
<router-outlet></router-outlet>
ng serve
{
path: '',
pathMatch: 'full',
redirectTo: 'plan/plan-home/procedure-list',
}
{
path: '**',
pathMatch: 'full',
redirectTo: 'plan/plan-home/procedure-list',
}
routerLinkActive:当前激活路由选中样式
routerLink:路由链接
<ul class="nav navbar-nav">
<li title="plan" >
<a routerLink="/plan/plan-home/procedure-details/details-home/plan">
<span [routerLinkActive]="['active']">plan</span>
</a>
</li>
<li title="design">
<a routerLink="/plan/plan-home/procedure-details/details-home/design/{
{id}}">
<span [routerLinkActive]="['active']">design</span>
</a>
<a [routerLink]="['/plan/plan-home/procedure-details/details-home/design', id]">
<span [routerLinkActive]="['active']">plan</span>
</a>
</li>
<li title="execution">
<a routerLink="/plan/plan-home/procedure-details/details-home/execution">
<span [routerLinkActive]="['active']">execution</span>
</a>
</li>
</ul>
然后把一个参数化路由添加到 AppRoutingModule.routes 数组中,它要匹配指向文章详情视图的路径。
{
path: 'design/:id',
loadChildren: () => import('./modules/details-design/details-design.module').then(m => m.DetailsDesignModule),
},
path 中的冒号(:)表示 :id 是一个占位符,它表示某个特定数据。
然后,我们需要修改HTML页面中的文章列表跳转链接
<li title="design">
<a routerLink="/plan/plan-home/procedure-details/details-home/design/{
{id}}">
<span [routerLinkActive]="['active']">design参数 </span>
</a>
<a [routerLink]="['/plan/plan-home/procedure-details/details-home/design', id]">
<span [routerLinkActive]="['active']">design参数</span>
</a>
</li>
先添加下列导入语句:
import {
Router } from '@angular/router';
然后把 ActivatedRoute注入到构造函数中,将它们的值保存到私有变量里
constructor(private router: Router) {
}
this.router.navigate(['/plan/plan-home/procedure-list']);
this.router.navigate(['/plan/plan-home/procedure-details/details-home/design', this.id]);
多个参数
this.router.navigate(['/plan/plan-home'], {
queryParams: {
productId: '1', title: 'moon' }});
相对路径
this.router.navigate(['../../../plan/plan-home/procedure-list']);
绝对路径
this.router.navigateByUrl('/plan/plan-home/procedure-list');
绝对错误路径
this.router.navigateByUrl('../../../plan/plan-home/procedure-list'); // error, no any match
跳转效果:
import {
ActivatedRoute } from '@angular/router';
constructor(
private route: ActivatedRoute,
) {
}
constructor(private route: ActivatedRoute) {
this.route.params.subscribe(
params => {
console.log(params);
}
);
}
方法二、
constructor(private route: ActivatedRoute) {
this.route.queryParams.subscribe(queryParams => {
console.log(queryParams.productId);
console.log(queryParams.title);
});
}
方法三、
console.log(this.route.snapshot.paramMap.get('id'));
console.log(this.route.snapshot.params['id']);
获取路由参数效果图:
import {
Location } from '@angular/common';
constructor(private location: Location) {
}
<button (click)="goBack()">返回到上一个</button>
ts
goBack() {
this.location.back();
}
通过点击浏览器的后退按钮,可以回到上一个也买你进入的页面。另外服务Location在浏览器的历史栈中后退一步。
路由嵌套github地址
路由配置
Angular官网