Angular路由器:为什么Router ActivatedRoute.children是一个数组/始终只有一个子级?
创始人
2024-10-28 04:30:15
0

在Angular中,当使用路由器进行导航时,可以使用ActivatedRoute来访问当前路由的信息,包括路由参数、查询参数等。而ActivatedRoute对象的children属性是一个包含当前路由的子路由的数组。

通常情况下,ActivatedRoute.children数组只有一个子级,这是因为路由配置中定义了一个子路由。如果路由配置中没有定义子路由,children数组将为空。但是,ActivatedRoute.children始终是一个数组,即使只有一个子级。

以下是一个示例来解释为什么ActivatedRoute.children是一个数组/始终只有一个子级的原因:

// app.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AppComponent } from './app.component';
import { HomeComponent } from './home.component';
import { AboutComponent } from './about.component';

const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'about', component: AboutComponent, children: [
    { path: 'child', component: ChildComponent }
  ]},
];

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

// about.component.ts
import { Component } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-about',
  template: `
    

About Page

` }) export class AboutComponent { constructor(private route: ActivatedRoute) { console.log(route.children); // [ActivatedRoute] } } // child.component.ts import { Component } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; @Component({ selector: 'app-child', template: `

Child Page

` }) export class ChildComponent { constructor(private route: ActivatedRoute) { console.log(route.children); // [] } }

在上面的示例中,我们定义了一个父级路由about和一个子级路由child。当访问/about时,AboutComponent会被加载,并且route.children将包含一个ActivatedRoute对象。而当访问/about/child时,ChildComponent会被加载,并且route.children将为空数组。

通过以上示例,你可以看到ActivatedRoute.children是一个数组,即使只有一个子级。你可以通过访问route.children[0]来获取子级的ActivatedRoute对象。

相关内容

热门资讯

三分钟了解!红龙poker辅助... 三分钟了解!红龙poker辅助工具,cloudpoker怎么开挂,本来真的是有挂(有挂实锤)亲,关键...
揭秘攻略!wepoker手机版... 揭秘攻略!wepoker手机版辅助,hh poker辅助有用吗,确实真的有挂(有挂头条)1、每一步都...
玩家爆料!wepoker辅助器... 玩家爆料!wepoker辅助器下载,wepoker透视辅助下载,好像真的是有挂(有挂方针)1、wep...
分辨真假!德普之星私人局透视,... 分辨真假!德普之星私人局透视,wepoker私人局辅助,一贯存在有挂(有挂分享)1、下载好透视辅助下...
推荐十款!wepoker到底有... 推荐十款!wepoker到底有没有透视,aapoker辅助软件合法吗,果然真的是有挂(有挂方法)1、...
盘点一款!购买的wpk辅助在哪... 盘点一款!购买的wpk辅助在哪里下载,wejoker辅助软件视频,本来真的是有挂(揭秘有挂)1)免费...
揭秘一下!we poker辅助... 揭秘一下!we poker辅助器,wepoker怎么发冤家牌,切实存在有挂(有挂技巧)1、每一步都需...
一分钟揭秘!wepoker数据... 一分钟揭秘!wepoker数据分析工具,wepoker怎么买辅助,竟然真的是有挂(有挂方略)透视方法...
研究成果!wepoker私人局... 研究成果!wepoker私人局俱乐部辅助,wepoker免费脚本弱密码,一贯真的有挂(有挂技巧)1、...
普及知识!aa poker辅助... 普及知识!aa poker辅助包,wepoker怎么看底牌,总是真的有挂(有挂细节)1、脚本辅助下载...