Ich möchte Layout für meine App erstellen. So verwende ich die folgenden Routen:Eckig 2. Hauptlayout für alle Routen
export const routes: Routes = [
{
path: '',
component: MainLayoutComponent,
children: [
{
path: '',
redirectTo: 'contact',
pathMatch: 'full'
},
{
path: 'contact',
loadChildren : 'app/contact/contact.module#ContactModule'
}
]
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}
Wie man sehen kann ich MainLayoutComponent
für das Layout. Und wenn meine Anwendung startet, möchte ich auf contact
Pfad umleiten.
Das Layout enthält Navigation und router-outlet
:
@Component({
template: `
<nav>Navigation content</nav>
<div class="container"><router-outlet></router-outlet></div>
`,
})
Dies ist Vorlage in ContactComponent
:
<h2>Contact Component</h2>
Das Problem ist, wenn app ausgeführt wird, ich Contact Component
Nachricht sehen, aber ich sehe nicht, Navigation. Aber, wenn ich localhost:3000/contact
öffne, sehe ich Navigation und Contact Component
Nachricht. Was vermisse ich?
Dies ist Plunker. Sie können überprüfen, ich create Go to Contact
Schaltfläche, um an /contact
URL umleiten.
Danke für die Antwort, nun, ich mache es auf diese Weise für eine bessere Anpassung. Wenn ich ein anderes Layout brauche, werde ich es einfach erstellen. Zum Beispiel für die Login/Registrierungsseite. – user348173
Aber ich kann nicht verstehen, warum route in ContactModule root url behandeln, registriere ich diese Route mit 'forChild' Methode. Kannst du mir das erklären? – user348173
Das liegt daran, dass die Routen, die mit forChild deklariert wurden, mit den Routen verbunden werden, die mit forRoot deklariert wurden. In Ihrem Fall erklären Sie die Route: { Pfad: '', RedirectTo: 'Kontakt', pathMatch: 'voll' }, { Pfad: 'Kontakt', loadChildren: ‚app/Kontakt/Kontakt. Modul # ContactModule ' } in der ForRoot und dann Sie {Pfad:' ', Komponente: ContactComponent} in der für Kind. Dies überschreibt wirksam { Pfad: '', redirectTo: 'Kontakt', pathMatch: 'voll' } weshalb Ihr Nav nicht angezeigt –