Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

angular - How to Route to Feature Module

I'm trying to have a route from one module include a component from another module. The scheme I'm trying to implement is: /group/feature which should show the content in feature.html included in the content of group.html where the router-outlet is. I can get /group to work, but /group/feature returns page not found.

The full project is available here: https://stackblitz.com/edit/angular-nsfgyr

Here are the main files:

feature.module.ts:

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [FeatureComponent],
  exports: [FeatureComponent]
})
export class FeatureModule {
}

group-module.ts

@NgModule({
  imports: [
    CommonModule,
    FeatureModule,
    GroupRoutingModule,
  ],
  declarations: [
    GroupComponent,
  ],
  exports: [GroupComponent]
})
export class GroupModule {
}

group-routing.module.ts:

const ingestRoutes: Routes = [
  {path: 'feature', component: FeatureComponent}
];

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

app-routing.module:

const routes: Routes = [
    {path: '', pathMatch: 'full', redirectTo: 'group'},
    {path: 'group', component: GroupComponent},
    {path: '**', component: PageNotFoundComponent}
  ]
;

@NgModule({
  imports: [
    RouterModule.forRoot(
      routes,
      {enableTracing: false, useHash: false}
    )
  ],
  exports: [
    RouterModule
  ]
})
export class AppRoutingModule {
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The first way. No children, no modules imported.

const routes: Routes = [
  {path: '', pathMatch: 'full', redirectTo: 'group'},
  {path: 'group/feature', component: FeatureComponent },
  {path: 'group', component: GroupComponent},
  {path: '**', component: PageNotFoundComponent}
];

@NgModule({
  imports: [
  RouterModule.forRoot(routes,
  {enableTracing: false, useHash: false}
  )
],
exports: [
  RouterModule
]
})
export class AppRoutingModule {
}

The second way is to use children..

const routes: Routes = [
  {path: '', pathMatch: 'full', redirectTo: 'group'},
  {path: 'group', component: GroupComponent,
   children: [
    { path: 'feature', component: FeatureComponent }]     
  },
  {path: '**', component: PageNotFoundComponent}
];

The third way is to import the module. It has its own

"<router-outlet></router-outlet>".

const ingestRoutes: Routes = [
  { path: 'group/feature', component: FeatureComponent,
    children: [       
      path: 'more', component: MoreComponent, // if you want to add..
        // this will route to group/feature/more
      path: '', component: FeatureHomeComponent,
    ] 
  }
];

@NgModule({
  imports: [
    RouterModule.forChild(ingestRoutes)
  ],
  declarations: [
      FeatureComponent, FeatureHomeComponent, MoreComponent
  ],
  exports: [
    RouterModule
  ]
})
export class GroupRoutingModule {
}

The FeatureComponent.html has only

<route-outlet></router-outlet>. 

FeatureHomeComponent.html

- you can display anything here..YOUR ACTURE FEATURE COMPONENT.HTML

Export this in AppRoutingModule.

 const routes: Routes = [
    {path: '', pathMatch: 'full', redirectTo: 'group'},
    {path: 'group', component: GroupComponent},
    {path: '**', component: PageNotFoundComponent}
  ];

@NgModule({
  imports: [
    RouterModule.forRoot(routes, {enableTracing: false, useHash: 
    false}), GroupRoutingModule, // add this..
  ],
  exports: [
    RouterModule
  ]
})
export class AppRoutingModule {
}

All I do for 'apple brand' is to just import AppleRoutingModule in my AppRoutingModule. See link.Angular 4 route selection for category/subcategory or category/item for ecommerce web site


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...