亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Angular中的ActivatedRoute和Router原理解釋

 更新時間:2023年04月16日 16:31:17   作者:Data_Adventure  
這篇文章主要為大家介紹了Angular中的ActivatedRoute和Router原理解釋,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

Angular中的ActivatedRoute和Router解釋

在Angular中,ActivatedRouteRouter是兩個核心的路由服務。他們都提供可以用來檢查和操作當前頁面路由信息的方法和屬性。

ActivatedRoute

ActivatedRoute是一個保存關于當前路由狀態(tài)(如路由參數(shù)、查詢參數(shù)以及其他數(shù)據(jù))的對象。 它可以讓開發(fā)人員從路由器中訪問路由參數(shù)和查詢參數(shù)。

ActivatedRoute是路由事件數(shù)據(jù)的載體。 這包括在導航期間收集的靜態(tài)和動態(tài)段以及查詢參數(shù)、Fragment等等。

例如,對于這個路由:

{ path: 'product/:id', component: ProductDetailComponent }

通過獲取ActivatedRoute,我們可以輕松地訪問id值:

import { Component } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
  selector: 'app-product-detail',
  template: 'Product Details Component'
})
export class ProductDetailComponent implements OnInit {
  constructor(private route: ActivatedRoute) {}
  ngOnInit() {
    const id = +this.route.snapshot.paramMap.get('id');
    // ...
  }
}

在上面的代碼示例中,ActivatedRoute通過注入該服務作為構造函數(shù)的參數(shù)而獲取。接下來,我們只需使用快照對象(即this.route.snapshot)就可以快速訪問路由參數(shù)。要獲取參數(shù)的特定值,可以使用get方法訪問params屬性,該方法采用一個字符串參數(shù)并返回一個字符串:

const id = +this.route.snapshot.paramMap.get('id');

這里的加號意味著我們將結果轉換為數(shù)字類型。

另一種訪問路由參數(shù)的方法是通過訂閱paramMap可觀察值。subscribe`方法定義給observable帶來副作用,就像任何** RxJS **observable一樣:

this.route.paramMap.subscribe(params => {
  const id = +params.get('id');
  // ...
});

這種方式允許動態(tài)更改URL。(你的組件不需要重新創(chuàng)建。)

Router

Router通過向前和向后導航和路由裝置提供了一種明顯且簡單的編程API,同時仍然保留完全配置的強大能力。

路由器是一個抽象的概念,它用于選擇輸入URL,并將其轉換為經(jīng)過測試的規(guī)則來獲取特定組件。 在Angular中,路由器是NgModule中的引導項之一。 路由器設置可能看起來非常困難,但是一旦了解了基本情況,它們就會感到自然。

基本導航

首先,我們根據(jù)常規(guī)用法配置Routes數(shù)組:

// app-routing.module.ts file
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { ProductListComponent } from './product-list/product-list.component';
import { ProductDetailComponent } from './product-detail/product-detail.component';
const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'products', component: ProductListComponent },
  { path: 'products/:id', component: ProductDetailComponent }
];
@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

我們設定了三個路由:空路徑(主頁),產(chǎn)品列表和特定ID的產(chǎn)品。 每個路徑都與對應的組件相關聯(lián)。

然后,我們在模板或組件類中安排具有相應路由聲明的鏈接:

<!-- home.component.html -->
<a routerLink="/">Home</a>
<a routerLink="/products">Product List</a>
<!-- product-list.component.html -->
<ul>
    <li *ngFor="let product of products">
        <a [routerLink]="['/products', product.id]">{{ product.name }}</a>
    </li>
</ul>
<!-- product-detail.component.html -->
<h2>Product Detail</h2>
<p>{{ product }}</p>

在上面的所有代碼示例中,我們使用了routerLink指令完成路由導航?,F(xiàn)在,當用戶點擊鏈接時,路由器會根據(jù)路徑加載相應的組件并在指令的位置動態(tài)渲染該組件。

以上就是Angular中的ActivatedRoute和Router原理詳解的詳細內(nèi)容,更多關于Angular ActivatedRoute Router的資料請關注腳本之家其它相關文章!

相關文章

  • webapp框架AngularUI的demo改造之路

    webapp框架AngularUI的demo改造之路

    這篇文章主要介紹了webapp框架AngularUI的demo改造之路,需要的朋友可以參考下
    2014-12-12
  • 在 Angular中 使用 Lodash 的方法

    在 Angular中 使用 Lodash 的方法

    這篇文章主要介紹了在 Angular中 使用 Lodash 的方法,需要的朋友可以參考下
    2018-02-02
  • Angular使用動態(tài)加載組件方法實現(xiàn)Dialog的示例

    Angular使用動態(tài)加載組件方法實現(xiàn)Dialog的示例

    這篇文章主要介紹了Angular使用動態(tài)加載組件方法實現(xiàn)Dialog的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • AngularJS基礎 ng-src 指令簡單示例

    AngularJS基礎 ng-src 指令簡單示例

    本文主要介紹AngularJS ng-src 指令,這里對ng-src 指令的資料做了詳細整理,有需要的小伙伴可以參考下
    2016-08-08
  • AngularJS基礎學習筆記之指令

    AngularJS基礎學習筆記之指令

    指令(Directives)是所有AngularJS應用最重要的部分。盡管AngularJS已經(jīng)提供了非常豐富的指令,但還是經(jīng)常需要創(chuàng)建應用特定的指令。這篇教程會為你講述如何自定義指令,以及介紹如何在實際項目中使用。
    2015-05-05
  • Angularjs使用指令做表單校驗的方法

    Angularjs使用指令做表單校驗的方法

    本篇文章主要介紹了Angularjs使用指令做表單校驗的方法,詳細的介紹了用指令做校驗的方法,具有一定的參考價值,有興趣的可以了解一下。
    2017-03-03
  • Angular6升級到Angular8報錯問題的解決合集

    Angular6升級到Angular8報錯問題的解決合集

    這篇文章主要介紹了Angular6升級到Angular8報錯問題的解決合集,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Angularjs驗證用戶輸入的字符串是否為日期時間

    Angularjs驗證用戶輸入的字符串是否為日期時間

    這篇文章主要介紹了Angularjs驗證用戶輸入的字符串是否為日期時間,需要的朋友可以參考下
    2017-06-06
  • Angular2使用jQuery的方法教程

    Angular2使用jQuery的方法教程

    這篇文章主要給大家介紹了關于Angular2使用jQuery的方法教程,文中介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來跟著小編一起學習學習吧。
    2017-05-05
  • 簡單談談require模塊化jquery和angular的問題

    簡單談談require模塊化jquery和angular的問題

    下面小編就為大家?guī)硪黄唵握務剅equire模塊化jquery和angular的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06

最新評論