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 { }

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

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

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

相关内容

热门资讯

WPK下载辅助器烘培!wepo... 您好,wepoker免费脚本咨询这款游戏可以开挂的,确实是有挂的,需要了解加去威信【13670430...
总算清楚!辅助器!扬帆ai辅助... 总算清楚!辅助器!扬帆ai辅助器多功能插件,从来有辅助器安装办法是一款可以让一直输的玩家,快速成为一...
WPK俱乐部透视挂攻略!德普之... WPK俱乐部透视挂攻略!德普之星有透视辅助吗,红龙PoKerai多功能透视挂工具1、德普之星有透视辅...
热点讨论开挂!微乐苏州麻将ai... 热点讨论开挂!微乐苏州麻将ai开挂多功能插件,真是真的是有开挂软件经验是一款可以让一直输的玩家,快速...
WPK购买透视挂方针!wepo... WPK购买透视挂方针!wepoker辅助工具,AApokerai多功能透视挂方法一、wepoker辅...
最新研发透视挂!太浪乾安麻将a... 很多人使用云端 AI 时,都担心太浪乾安麻将、内部资料上传带来隐私风险。这款本地运行的 AI 黑科技...
WPK辅助透视挂步骤!微扑克游... WPK辅助透视挂步骤!微扑克游戏辅助器,菠萝智星德州ai多功能透视挂攻略1、进入到微扑克游戏辅助器是...
科普开挂!西元红河棋牌ai开挂... 科普开挂!西元红河棋牌ai开挂多功能插件,先前真的有开挂软件方案是一款可以让一直输的玩家,快速成为一...
WPK多开辅助器大纲!wepo... WPK多开辅助器大纲!wepoker有辅助功能吗,wpkai多功能辅助器攻略1、首先打开wepoke...
我来教教你辅助挂!麻友圈ai辅... 很多人使用云端 AI 时,都担心麻友圈、内部资料上传带来隐私风险。这款本地运行的 AI 黑科技,很好...