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将会被渲染在父组件中。

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

相关内容

热门资讯

攻略讲解!智星德州可以透视吗,... 攻略讲解!智星德州可以透视吗,wepoker软件靠谱么,wepoke教程(有挂教程)1、许多玩家不知...
透视安卓版!we-poker有... 透视安卓版!we-poker有人玩吗(透视)原来是真的有挂(详细辅助教你攻略);辅助器中分为三种模型...
解密关于!wepoker软件靠... 解密关于!wepoker软件靠谱么,pokemmo辅助工具,微扑克教程(有挂教程);《WPK辅助透视...
透视能赢!aapoker免费透... 透视能赢!aapoker免费透视脚本(透视)竟然是真的有挂(详细辅助扑克教程);在进入aapoker...
免费测试版!xpoker辅助控... 免费测试版!xpoker辅助控制,德普辅助器辅助器怎么用,爆料教程(有挂辅助)科技教程也叫必备教程,...
透视智能ai!wepoker破... 透视智能ai!wepoker破解是真的还是假的(透视)真是有挂(详细辅助必胜教程)1、起透看视 透明...
分享一款!pokemmo辅助工... 分享一款!pokemmo辅助工具,佛手在线有挂吗,AI教程(有挂技巧)准备好在佛手在线有挂吗 ia的...
透视黑科技!wpk透视辅助(透... 透视黑科技!wpk透视辅助(透视)一贯有挂(详细辅助德州论坛)1、wpk透视辅助ai辅助优化,wpk...
总算清楚!拱趴大菠萝机器人,a... 1、总算清楚!拱趴大菠萝机器人,aapoker脚本怎么用,揭秘教程(有挂辅助)。2、拱趴大菠萝机器人...
透视黑科技!wepoker数据... 透视黑科技!wepoker数据分析工具(透视)切实真的是有挂(详细辅助插件教程);1、机器人多个强度...