欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Angular4 路由設(shè)置相關(guān)

 更新時(shí)間:2017年08月26日 09:44:42   作者:壹頁書  
本篇文章主要介紹了詳解Angular4 路由設(shè)置相關(guān),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

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

路由類設(shè)置

/*路由基本模型*/

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

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

/*將路由設(shè)置導(dǎo)出,子模塊中的路由使用 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/ 時(shí)同時(shí)加載 NewsComponent News2Cmponent 兩個(gè)組件

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" 即在本路由激活時(shí)添加樣式 .active

或者:

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

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

4.路由守衛(wèi)(適用于后臺(tá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
 ) {
  // 目標(biāo)路由和當(dāng)前路由
  console.log(activatedRouteSnapshot);
  console.log(routerStateSnapshot);

  // 判斷并返回
  return canDeactivateComponent.canDeactivate ? canDeactivateComponent.canDeactivate() : true

 }
}

..

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

路由配置

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

模塊中添加服務(wù)

providers: [
 DeactivateGuardService
]

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Angular5中狀態(tài)管理的實(shí)現(xiàn)

    Angular5中狀態(tài)管理的實(shí)現(xiàn)

    這篇文章主要介紹了Angular5中狀態(tài)管理的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-09-09
  • AngularJS中處理多個(gè)promise的方式

    AngularJS中處理多個(gè)promise的方式

    promise是一種用異步的方式處理值的方法,promise是對象,代表了一個(gè)函數(shù)最終可能的返回值或者拋出的異常,在與遠(yuǎn)程對象打交道時(shí)我們可以把他看作是遠(yuǎn)程對象的一個(gè)代理,通過本文給大家介紹AngularJS中處理多個(gè)promise的方式及什么是promise,需要的朋友參考下
    2016-02-02
  • angular.bind使用心得

    angular.bind使用心得

    這篇文章主要介紹了angular.bind使用心得,以及個(gè)人對于angular.bind的理解,這里分享給大家,希望大家能夠喜歡。
    2015-10-10
  • 用WebStorm進(jìn)行Angularjs 2開發(fā)(環(huán)境篇:Windows 10,Angular-cli方式)

    用WebStorm進(jìn)行Angularjs 2開發(fā)(環(huán)境篇:Windows 10,Angular-cli方式)

    這篇文章主要介紹了用WebStorm進(jìn)行Angularjs 2開發(fā)(環(huán)境篇:Windows 10,Angular-cli方式),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-12-12
  • AngularJs中route的使用方法和配置

    AngularJs中route的使用方法和配置

    angular是Google開發(fā)的一個(gè)單頁面應(yīng)用框架,是現(xiàn)在比較主流的單頁面應(yīng)用框架之一,下面通過本文給大家介紹AngularJs中route的使用方法和配置,感興趣的朋友一起學(xué)習(xí)吧
    2016-02-02
  • AngularJS實(shí)現(xiàn)的JSONP跨域訪問數(shù)據(jù)傳輸功能詳解

    AngularJS實(shí)現(xiàn)的JSONP跨域訪問數(shù)據(jù)傳輸功能詳解

    這篇文章主要介紹了AngularJS實(shí)現(xiàn)的JSONP跨域訪問數(shù)據(jù)傳輸功能,較為詳細(xì)的分析了JSONP的概念、功能并結(jié)合實(shí)例形式給出了AngularJS使用JSONP進(jìn)行跨域訪問數(shù)據(jù)傳輸?shù)南嚓P(guān)技巧,需要的朋友可以參考下
    2017-07-07
  • 解決Angular.js中使用Swiper插件不能滑動(dòng)的問題

    解決Angular.js中使用Swiper插件不能滑動(dòng)的問題

    下面小編就為大家分享一篇解決Angular.js中使用Swiper插件不能滑動(dòng)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • AngularJS入門教程之REST和定制服務(wù)詳解

    AngularJS入門教程之REST和定制服務(wù)詳解

    本文主要講解 AngularJS REST和定制服務(wù)的知識(shí),這里幫大家整理了相關(guān)知識(shí),并附示例代碼,詳細(xì)講解了RES和定制服務(wù)的知識(shí),有興趣的小伙伴可以參考下
    2016-08-08
  • AngularJS遍歷獲取數(shù)組元素的方法示例

    AngularJS遍歷獲取數(shù)組元素的方法示例

    這篇文章主要介紹了AngularJS遍歷獲取數(shù)組元素的方法,涉及AngularJS簡單的數(shù)組遍歷及元素獲取相關(guān)操作技巧,需要的朋友可以參考下
    2017-11-11
  • AngularJS使用ng-repeat指令實(shí)現(xiàn)下拉框

    AngularJS使用ng-repeat指令實(shí)現(xiàn)下拉框

    這篇文章主要介紹了AngularJS使用ng-repeat指令實(shí)現(xiàn)下拉框的相關(guān)資料,非常不錯(cuò),感興趣的朋友一起看下吧,需要的朋友可以參考下
    2016-08-08

最新評論