angular4路由 晓得这些就够用了

angular 4 路由

运用cli敕令建立根路由模块 ng g cl app.router 或本身建一个路由设置文件 如:app/app.router.ts

// app/app.router.ts
// 将文件修正成

import { RouterModule, Routes } from '@angular/router';

const routes: Routes = [
  // 今后在这里修正设置
  { path: '**', pathMatch: 'full', redirectTo: '' }
];
// 将路由设置导出为 appRouting 以供挪用, 子模块中的路由运用 forChild 而不是 forRoot
export const appRouting = RouterModule.forRoot(routes);

在根模块中引入路由, 为特征模块定义的路由在其模块中引入

// app/app.module.ts
import { appRouting } from "./router/router.module"
// @NgModule({
//  imports: [ ... ,
  appRouting
// ...

路由设置

const routes: Routes = [
  // path:途径 /news component:组件
  { path: 'news',  component: Newsomponent },
  // path:途径 /detail/1 component:组件
  { path: 'detail/:id', component: DetailComponent },
  // 懒加载子模块, 子模块须要设置路由设置启动子组件
  { path: 'other', loadChildren:"./other/other.module#otherModule" },
  // 重定向
  { path: '**', pathMatch: 'full', redirectTo: '' }
];
  • pathMatch?: string; 默以为前缀婚配 “prefix”; “full” 为完全婚配

  • outlet?: string; 路由目的

  • children?: Routes; 子路由的划定规矩

加载路由

在根组件或当前组件的模板中

<router-outlet></router-outlet>

多个路由地区

  { path: 'news', outlet:'let1'  component: NewsComponent }
  { path: 'news', outlet:'let2'  component: News2Cmponent }
<router-outlet name="let1"></router-outlet>
<router-outlet name="let2"></router-outlet>

即接见 /news/ 时同时加载 NewsComponentNews2Cmponent 两个组件

链接及接见

<a routerLink="/detail/1" routerLinkActive="active">detail</a>
<a [routerLink]="['/detail', news.id]">{{news.title}}</a>
<a [routerLink]="[{ outlets: { let2: ['news'] } }]">Contact</a>

routerLinkActive="active" 即在本路由激活时增添款式 .active

import { Router } from '@angular/router';
// ...
constructor(private router: Router) {}

// ...

this.router.navigate(['/detail', this.news.id])
this.router.navigate([{ outlets: { let2: null }}]);

navigateByUrl 要领指向完全的绝对途径

路由守御

适用于背景治理等须要登录才运用的模块

建立一个认证效劳

// app/auth.service.ts

import { Injectable }     from '@angular/core';
import { CanActivate }    from '@angular/router';

@Injectable()
export class AuthService implements CanActivate {
  canActivate() {
    // 这里推断登录状况, 返回 true 或 false
    return true;
  }
}

增添或修正路由设置

// app/app.router.ts

// 增添 CanActivate
import { CanActivate ... } from '@angular/router';


  // 设置中增添 canActivate 如:
  { path: 'admin', canActivate:[AuthService] ... }

退出守御

适合于编辑器修正后的保留提醒等场景

// app/deac.service.ts

import { Injectable }     from '@angular/core';
import { CanDeactivate, ActivatedRouteSnapshot, RouterStateSnapshot }    from '@angular/router';

// CanDeactivateComponent 是定义的接口,见下段代码
import { CanDeactivateComponent } from './can-deactivate.omponent';

@Injectable()
export class DeacService implements CanDeactivate<CanDeactivateComponent> {
  canDeactivate(
    canDeactivateComponent: CanDeactivateComponent,
    activatedRouteSnapshot: ActivatedRouteSnapshot,
    routerStateSnapshot: RouterStateSnapshot
  ) {
    // 目的路由和当前路由
    console.log(activatedRouteSnapshot);
    console.log(routerStateSnapshot);

    // 推断并返回
    return canDeactivateComponent.canDeactivate ? canDeactivateComponent.canDeactivate() : true

  }
}
// can-deactivate.omponent.ts

// 接口组件, 返回 true 或 false 如表单发作转变则挪用对话框效劳
export interface CanDeactivateComponent {
  canDeactivate: () => Observable<boolean> | Promise<boolean> | boolean;
}

路由设置

{
  path: ...,
  canDeactivate: [DeacService],
  component: ...
}

模块中增添效劳

providers: [
  DeactivateGuardService
]
    原文作者:裁缝
    原文地址: https://segmentfault.com/a/1190000009357922
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞