欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Angular4路由設(shè)置的示例分析

這篇文章主要介紹Angular4路由設(shè)置的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)公司長期為成百上千家客戶提供的網(wǎng)站建設(shè)服務,團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為旬陽企業(yè)提供專業(yè)的做網(wǎng)站、網(wǎng)站制作旬陽網(wǎng)站改版等技術(shù)服務。擁有十多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

1.路由相關(guān)配置

路由類設(shè)置

/*路由基本模型*/

/*導入RouterModule,Routes類型*/
import { RouterModule, Routes } from '@angular/router';
import { LoginComponent } from "./login/login.component";

/*定義路由const表示不可改變*/
const routers: Routes = [
 /*
  path:字符串,表示默認登入,
  path為路徑 /login component:組件
  component:組件
  pathMatch:為字符串默認為前綴匹配 "prefix"; "full" 為完全匹配。
  redirectTo:指向為路徑,既path
  outlet:字符串,路由目標,面對多個路由的情況
  children:Routes 子路由相關(guān)
  */
 { path: '', component: LoginComponent },
 // path:路徑 /detail/1 :id代表參數(shù)相關(guān)
 { path: 'detail/:id', component: LoginComponent },
 // 懶加載子模塊, 子模塊需要配置路由設(shè)置啟動子組件,如果這樣設(shè)置了路由,需要在子模塊中再定義路由
 { path: 'other', loadChildren:"./demo/demo.module#demoModule" },
 // 重定向,路徑為** 表示不能識別的路徑信息,重定向到相關(guān)路徑下
 { path: '**', pathMatch: 'full', redirectTo: '' }
];

/*將路由設(shè)置導出,子模塊中的路由使用 forChild 而不是 forRoot*/
export const appRouter = RouterModule.forRoot(routers);

ngModule設(shè)置

@NgModule({
 declarations: [
 ......
 ],
 imports: [
 ......
  appRouter
 ]
})

組件模板設(shè)置

<router-outlet></router-outlet>

2.多路由處理

{ 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/ 時同時加載 NewsComponent News2Cmponent 兩個組件

3.路有鏈接以及組件中調(diào)用路由方法使用

<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

或者:

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

其中:navigateByUrl 方法指向完整的絕對路徑

4.路由守衛(wèi)(適用于后臺管理等需要登錄才能使用的模塊)

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

@Injectable()
export class AuthService implements CanActivate {
 canActivate() {
  // 這里判斷登錄狀態(tài), 返回 true 或 false
  return true;
 }
}

在路由配置中的設(shè)置

{ path: '', component: LoginComponent, canActivate:[LoginComponent] },

5.退出守衛(wèi)(適合于編輯器修改后的保存提示等場景)

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

 }
}

..

// 接口組件, 返回 true 或 false 如表單發(fā)生改變則調(diào)用對話框服務
export interface CanDeactivateComponent {
 canDeactivate: () => Observable<boolean> | Promise<boolean> | boolean;
}

路由配置

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

模塊中添加服務

providers: [
 DeactivateGuardService
]

以上是“Angular4路由設(shè)置的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

本文標題:Angular4路由設(shè)置的示例分析
文章源于:http://www.chinadenli.net/article8/gghoip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)服務器托管網(wǎng)站排名靜態(tài)網(wǎng)站企業(yè)建站小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站