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

詳解Angular路由之子路由

 更新時間:2021年05月24日 10:16:19   作者:starof  
本文將介紹Angular子路由的用法,對此感興趣的同學(xué),可以參考下

一、子路由語法

二、實例

在商品詳情頁面,除了顯示商品id信息,還顯示了商品描述,和銷售員的信息。

通過子路由實現(xiàn)商品描述組件和銷售員信息組件展示在商品詳情組件內(nèi)部。

1、新建2個組件修改其內(nèi)容

ng g component productDesc
ng g component sellerInfo

重點是修改銷售員信息組件,顯示銷售員ID。

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-seller-info',
  templateUrl: './seller-info.component.html',
  styleUrls: ['./seller-info.component.css']
})
export class SellerInfoComponent implements OnInit {
  private sellerId: number;
  constructor(private routeInfo: ActivatedRoute) { }

  ngOnInit() {
    this.sellerId = this.routeInfo.snapshot.params["id"];
  }

}

2、修改路由配置

給商品組件加上子路由

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

3、修改product.component.ts的模版

注意:routerLink里要配置成./,不能再用/。

<p>
  這里是商品信息組件
</p>
<p>
  商品id是: {{productId}}
</p>

<a [routerLink]="['./']">商品描述</a>
<a [routerLink]="['./seller',99]">銷售員信息</a>
<router-outlet></router-outlet>

效果:

主路由是/product/2,子路由為空字符串:

主路由的商品詳情組件顯示出來了,子路由的空字符串對應(yīng)的商品描述組件也顯示出來了。

點銷售員信息鏈接:

URL路徑變成:http://localhost:4201/product/2/seller/99。

子路由seller/99,對應(yīng)的sellerInfo組件也展示出來。

注意:

1、插座router-out形成父子關(guān)系,可以無限嵌套

2、所有的路由信息都是在模塊層,在app.routing.module.ts中配置的。

路由信息都是在模塊層,所有的組件本身,并不知道任何跟路由相關(guān)的信息。

插座之間的父子關(guān)系——子路由。

插座之間的兄弟關(guān)系——輔助路由。

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

相關(guān)文章

  • AngularJS的表單使用詳解

    AngularJS的表單使用詳解

    這篇文章主要介紹了AngularJS的表單使用詳解,在JS原有的基礎(chǔ)上提供了更多與HTML交互的功能,需要的朋友可以參考下
    2015-06-06
  • 基于Angularjs實現(xiàn)分頁功能

    基于Angularjs實現(xiàn)分頁功能

    這篇文章主要介紹了基于Angularjs實現(xiàn)分頁功能的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-05-05
  • Bootstrap與Angularjs的模態(tài)框?qū)嵗a

    Bootstrap與Angularjs的模態(tài)框?qū)嵗a

    這篇文章主要介紹了Bootstrap與Angularjs的模態(tài)框?qū)嵗a,需要的朋友可以參考下
    2017-08-08
  • Angular-Touch庫用法示例

    Angular-Touch庫用法示例

    這篇文章主要介紹了Angular-Touch庫用法,結(jié)合觸屏滑動事件的實現(xiàn)為例分析了Angular-Touch庫的相關(guān)使用技巧,需要的朋友可以參考下
    2016-12-12
  • AngularJs expression詳解及簡單示例

    AngularJs expression詳解及簡單示例

    本文主要介紹AngularJs expression,這里整理了詳細(xì)的資料,并附示例代碼,有興趣的小伙伴可以參考下
    2016-09-09
  • AngularJS快速入門

    AngularJS快速入門

    本文通過幾個循序漸進(jìn)的例子,給大家詳細(xì)講解了如何快速入門AngularJS,十分的實用,這里推薦給大家,有需要的小伙伴可以參考下。
    2015-04-04
  • angular2使用簡單介紹

    angular2使用簡單介紹

    Angular2開發(fā)者預(yù)覽版出來已有一段時間,這個以速度與移動性能為目的的框架到底如何,今天我們來結(jié)合官網(wǎng)的demo嘗試一下。
    2016-03-03
  • Angular之指令Directive用法詳解

    Angular之指令Directive用法詳解

    本篇文章主要介紹了Angular之指令Directive系列詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • Angular實現(xiàn)搜索框及價格上下限功能

    Angular實現(xiàn)搜索框及價格上下限功能

    這篇文章主要為大家詳細(xì)介紹了Angular實現(xiàn)搜索框及價格上下限功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 深入理解Angularjs中$http.post與$.post

    深入理解Angularjs中$http.post與$.post

    本篇文章主要介紹了深入理解Angularjs中$http.post與$.post ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05

最新評論