Angular路由子路由需要放在/目录下。
创始人
2024-10-28 06:00:55
0

在Angular中,子路由需要放在父路由的组件中,并且父路由的路径应该以斜杠(/)开头。以下是一个包含代码示例的解决方法:

首先,在父组件的HTML模板中,添加一个标签,用于渲染子路由的组件。例如,假设父组件的路径为/parent


这是父组件

然后,在父组件的路由配置中,定义子路由。可以在children属性中配置子路由的路径和对应的组件。例如,假设有两个子路由分别位于/parent/child1/parent/child2

// parent-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { ParentComponent } from './parent.component';
import { Child1Component } from './child1.component';
import { Child2Component } from './child2.component';

const routes: Routes = [
  {
    path: 'parent',
    component: ParentComponent,
    children: [
      { path: 'child1', component: Child1Component },
      { path: 'child2', component: Child2Component }
    ]
  }
];

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

最后,在父组件的模块中导入并添加ParentRoutingModuleimports数组中:

// parent.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { ParentComponent } from './parent.component';
import { Child1Component } from './child1.component';
import { Child2Component } from './child2.component';
import { ParentRoutingModule } from './parent-routing.module';

@NgModule({
  declarations: [
    ParentComponent,
    Child1Component,
    Child2Component
  ],
  imports: [
    CommonModule,
    ParentRoutingModule
  ]
})
export class ParentModule { }

现在,当访问/parent路径时,父组件将会被渲染,并且子组件的内容将会在标签中进行渲染。例如,当访问/parent/child1时,Child1Component将会被渲染在父组件中。

希望以上解决方法能对你有所帮助!

相关内容

热门资讯

wepower有外挂!微扑克超... wepower有外挂!微扑克超级统计,wepoke防外挂(详细透视教程)准备好在wepower有外挂...
wepoke辅助德之星!wep... wepoke辅助德之星!wepoke有挂,wepower伙牌,玩家教你(有挂技巧)是一款可以让一直输...
德州ai辅助软件!德扑之星用刷... 德州ai辅助软件!德扑之星用刷数据,nzt德州总是有挂(详细ai辅助教程)是一款可以让一直输的玩家,...
wpk有外 挂!wpk开挂实锤... wpk有外 挂!wpk开挂实锤(wpK)原来真的有挂-详细透视辅助教程;1、很好的工具软件,可以解锁...
德州之星插件!wpk有,红龙扑... 德州之星插件!wpk有,红龙扑克可以看底牌(详细辅助教程) 科技详细教程;757446909。新买了...
德扑数据软件!德州wpk到底有... 德扑数据软件!德州wpk到底有外 挂,来玩德州app真是真的是有挂(详细智能教程)1、下载好德扑数据...
黑科技教程!wepoke软件透... 黑科技教程!wepoke软件透明是真的(wepoke辅助德之星)WepOke(其实真的有挂);(需添...
wpk有外 挂!wpk开挂实锤... wpk有外 挂!wpk开挂实锤,WPK其实真的有挂,详细教程,(有挂揭秘);1、WPK系统规律教程、...
wepoke插件!微扑克真的有... wepoke插件!微扑克真的有外挂嘛,微扑克脚本代写(详细透视教程);1分钟了解详细教程(微信 13...
德扑之星猫腻!德州之星辅助器多... 1、德扑之星猫腻!德州之星辅助器多少钱,wpk德州一直是真的有挂(详细助手教程);该软件可以轻松地帮...