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对象。

相关内容

热门资讯

透视代打(wepoke是真的有... 透视代打(wepoke是真的有挂)wpk透视辅助可测试真的假的(详细辅助透明挂教程)确实真的是有挂是...
德扑之星ai代打!wepoke... 德扑之星ai代打!wepoke最新下载地址,(wEPoke)本然存在有挂(详细辅助必赢方法);德扑之...
透视辅助(wepoke辅助挂)... 1、透视辅助(wepoke辅助挂)微扑克软件发牌原理(详细辅助细节方法)一贯真的有挂;代表性(透视辅...
wepoke辅助机器人!云扑克... wepoke辅助机器人!云扑克德州有挂吗,(aapoKER)先前有挂(详细透视必胜教程)1、点击下载...
透视最新(德扑ai智能)pok... 透视最新(德扑ai智能)poker辅助工具(详细辅助软件教程)确实有挂是一款可以让一直输的玩家,快速...
wepoke插件!wepoke... wepoke插件!wepoke支持安卓吗,(wepOkE)最初真的是有挂(详细透视分享教程)科技教程...
透视总结(wepower辅助器... 您好,wepower辅助器这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩...
德扑ai智能机器人!智星德州菠... 德扑ai智能机器人!智星德州菠萝安全吗,(wopoker)真是是有挂(详细辅助实用技巧)准备好在德扑...
透视玄学(WePoKe透明挂)... 透视玄学(WePoKe透明挂)aa poker有外挂吗(详细辅助可靠技巧)原本存在有挂;透视玄学(W...
wepower辅助器!德扑胜率... wepower辅助器!德扑胜率最低的牌,(手机德州)原本存在有挂(详细辅助线上教程)1、许多玩家不知...