Run a Service Function inside an Angular Routing Module class

依然范特西╮ 提交于 2019-11-29 18:15:38

I've done something similar whereby I have a component with a menu. I want to show some components without that menu, such as the login component.

I did it using multiple levels of router outlets.

App Component

I defined the root application component with only a router outlet. Then I route into that component when I want a component to appear without the menu.

<router-outlet></router-outlet>

Shell Component

Then I defined a "shell" component with a second router outlet. Here is where I defined my menu. Anything I want to appear with the menu, I route to this router outlet.

<mh-menu></mh-menu>
<div class='container'>
   <router-outlet></router-outlet>
</div>

Routing Module

The routes are then configured using the children property to define the routes that are routed into the ShellComponent.

That way none of the components need to know if the menu should be on or off. It's all determined by the route configuration.

RouterModule.forRoot([
  {
    path: '',
    component: ShellComponent,
    children: [
      { path: 'welcome', component: WelcomeComponent },
      { path: 'movies', component: MovieListComponent },
      { path: '', redirectTo: 'welcome', pathMatch: 'full' }
    ]
  },
  { path: 'login', component: LoginComponent }
  { path: '**', component: PageNotFoundComponent }
])

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!