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

詳解Angular之路由基礎(chǔ)

 更新時間:2021年05月24日 09:57:56   作者:starof  
單頁應(yīng)用中,組件時構(gòu)建應(yīng)用的基礎(chǔ)元素,頁面展示什么內(nèi)容均是靠頁面有什么組件決定的,而展示什么組件又是由一組路由(帶有Url元素的特定集合,可用于導(dǎo)航視圖)決定的,希望本文可以幫助讀者了解路由的基礎(chǔ)概念和基礎(chǔ)使用、寫法。

一、路由相關(guān)對象

Router和RouterLink作用一樣,都是導(dǎo)航。Router是在Controller中用的,RouterLink是在模版中用到。

二、路由對象的位置

1、Routes對象

配置在模塊中。Routes由一組配置信息組成,每個配置信息至少包含兩個屬性,Path和Component。

2、RouterOutlet

在模版中

3、RouterLink

指令,在模版中生成鏈接改變URL

4、Router

在Controller中,調(diào)用Router對象的navigate方法,路由切換。

5、ActivatedRoute

路由時候通過URL傳遞數(shù)據(jù),數(shù)據(jù)會保存在ActivatedRoute對象中。

三、路由配置

使用ng new --routing參數(shù)時候會多生成出來一個app-routing.module.ts文件

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

const routes: Routes = [];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

會自動imports到app.module.ts中。

生成兩個組件home組件和component組件。

const routes: Routes = [
  {path: '', component : HomeComponent}, //路徑為空
  {path: 'product', component: ProductComponent}
];

注意:

1、path路徑配置不能以斜杠開頭,不能配置成path:'/product'。

因為Angular路由器會解析和生成url,不用/開頭是為了在多個視圖之間導(dǎo)航時能自由的使用相對路徑和絕對路徑。

2、在模版中寫路徑時,必須用/開頭。

因為用斜杠加.表示要導(dǎo)航到根路由(/)還是子路由(./)。

/就是導(dǎo)航到根路由,從配置根路由那一層找。

<a [routerLink]="['/']">主頁</a>

3、在<router-outlet>下面顯示組件內(nèi)容

4、routerLink參數(shù)是一個數(shù)組而不是字符串

因為在路由時候可以傳遞參數(shù)。

四、代碼中通過Router對象導(dǎo)航

模版上加一個按鈕

<input type="button" value="商品詳情" (click)="toProductDetails()">

controller中使用router.navigate導(dǎo)航。

navigate參數(shù)和routerLink參數(shù)配置一樣。

import { Component } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  constructor(private router:Router){
  }
  toProductDetails(){
    this.router.navigate(['/product']);
  }
}

點按鈕和點鏈接效果一樣。

五、配置不存在的路徑

生成code404組件顯示頁面不存在。

路由匹配先匹配者優(yōu)先,所以**通配符路由要放最后。

const routes: Routes = [
  { path: '', component: HomeComponent }, //路徑為空
  { path: 'product', component: ProductComponent },
  { path: '**', component: Code404Component }
];

六、重定向路由

一個地址重定向到另一個指定組件

www.aaa.com => www.aaa.com/products

www.aaa.com/x => www.aaa.com/y 用戶可能已經(jīng)收藏了x地址。

用重定向路由

const routes: Routes = [
  { path: '', redirectTo : 'home', pathMatch:'full' }, //路徑為空
  { path: 'home', component: HomeComponent },
  { path: 'product', component: ProductComponent },
  { path: '**', component: Code404Component }
];

七、在路由時候傳遞數(shù)據(jù)

有3種方式

1、在查詢參數(shù)中傳遞數(shù)據(jù)

2、在路由路徑中傳遞數(shù)據(jù)

定義路由路徑時就要指定參數(shù)名字,在實際路徑中攜帶參數(shù)。

3、在路由配置中傳遞數(shù)據(jù)

以上就是詳解Angular之路由基礎(chǔ)的詳細(xì)內(nèi)容,更多關(guān)于Angular之路由基礎(chǔ)的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • AngularJS 如何在控制臺進(jìn)行錯誤調(diào)試

    AngularJS 如何在控制臺進(jìn)行錯誤調(diào)試

    本文主要介紹AngularJS 如何在控制臺進(jìn)行錯誤調(diào)試,還不錯,分享給大家,希望給大家做一個參考。
    2016-06-06
  • angularjs請求數(shù)據(jù)的方法示例

    angularjs請求數(shù)據(jù)的方法示例

    這篇文章主要給大家介紹了關(guān)于angularjs請求數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用angularjs具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • angular 基于ng-messages的表單驗證實例

    angular 基于ng-messages的表單驗證實例

    本篇文章主要介紹了angular 基于ng-messages的表單驗證實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • Angular ng-repeat 對象和數(shù)組遍歷實例

    Angular ng-repeat 對象和數(shù)組遍歷實例

    這篇文章主要介紹了Angular ng-repeat對象和數(shù)組遍歷的相關(guān)資料,并附代碼示例,需要的朋友可以參考下
    2016-09-09
  • AngularJs bootstrap詳解及示例代碼

    AngularJs bootstrap詳解及示例代碼

    本文主要講解AngularJs bootstrap,這里整理了詳細(xì)的資料及簡單示例代碼幫助大家學(xué)習(xí)或者參考,有需要的小伙伴看下
    2016-09-09
  • 深究AngularJS如何獲取input的焦點(自定義指令)

    深究AngularJS如何獲取input的焦點(自定義指令)

    本篇文章主要介紹了AngularJS如何獲取input的焦點(自定義指令),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • AngularJS1.X學(xué)習(xí)筆記2-數(shù)據(jù)綁定詳解

    AngularJS1.X學(xué)習(xí)筆記2-數(shù)據(jù)綁定詳解

    本篇文章主要介紹了AngularJS1.X學(xué)習(xí)筆記2-數(shù)據(jù)綁定詳解,具有一定的參考價值,有興趣的可以了解一下。
    2017-04-04
  • Angular項目里ngsw-config.json文件作用詳解

    Angular項目里ngsw-config.json文件作用詳解

    這篇文章主要為大家介紹了Angular項目里ngsw-config.json文件作用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • 妙用Angularjs實現(xiàn)表格按指定列排序

    妙用Angularjs實現(xiàn)表格按指定列排序

    使用AngularJS的過濾器,可以很容易的實現(xiàn)在表格中,點擊某一列標(biāo)題進(jìn)行排序,實現(xiàn)代碼也很簡單,下面小編給大家分享angularjs實現(xiàn)表格按指定列排序的實現(xiàn)代碼,需要的的朋友參考下吧
    2017-06-06
  • Angular中的ng-template及angular 使用ngTemplateOutlet 指令的方法

    Angular中的ng-template及angular 使用ngTemplateOutlet 指令的方法

    ng-template 是用來定義模板的,當(dāng)使用ng-template定義好一個模板之后,可以用ng-container和templateOutlet指令來進(jìn)行使用。這篇文章給大家介紹了Angular中的ng-templateangular及使用 ngTemplateOutlet 指令的方法,需要的朋友參考下吧
    2018-08-08

最新評論