最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Angular路由之子路由

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

一、子路由語(yǔ)法

二、實(shí)例

在商品詳情頁(yè)面,除了顯示商品id信息,還顯示了商品描述,和銷(xiāo)售員的信息。

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

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

ng g component productDesc
ng g component sellerInfo

重點(diǎn)是修改銷(xiāo)售員信息組件,顯示銷(xiāo)售員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]">銷(xiāo)售員信息</a>
<router-outlet></router-outlet>

效果:

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

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

點(diǎn)銷(xiāo)售員信息鏈接:

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

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

注意:

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

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

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

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

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

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

相關(guān)文章

最新評(píng)論

育儿| 桐梓县| 连江县| 竹北市| 清水河县| 翁牛特旗| 西丰县| 黔西县| 双江| 宿松县| 峨眉山市| 安达市| 宁阳县| 武汉市| 永福县| 道孚县| 叙永县| 沅江市| 云龙县| 延长县| 洱源县| 泸溪县| 舞阳县| 平邑县| 孙吴县| 秦皇岛市| 阜城县| 达孜县| 修文县| 修武县| 麻城市| 浮山县| 康乐县| 垫江县| 新沂市| 阿鲁科尔沁旗| 股票| 同德县| 铅山县| 淳安县| 清远市|