2016-09-04 19 views
5

Nie mogę wydawać się routing do pracy z dziećmi w Angular 2. Aplikacja jest moją pierwszą aplikacją Angular 2, jest bardzo prosta i ma pasek nawigacji na górze, który powinien wypełnić zawartość w dolnej części strony. Ilekroć klikam którekolwiek z linków nawigacyjnych, wszystkie nawigują do tego samego dziecka, a co gorsza, zawartość tego dziecka się układa, czyli klikam raz, dziecko ładuje się poniżej, klikam ponownie, ładuje się ponownie poniżej, więc mam je dwa razy, i tak dalej w nieskończoność.Routing Angular 2 nie działa poprawnie

Pełna treść można znaleźć tutaj: plunker

To jest mój plik searches.routing.ts:

import { ModuleWithProviders } from '@angular/core'; 
import { Routes, RouterModule } from '@angular/router'; 

import { AdvancedSearchComponent } from './advanced-search/advanced-search.component'; 
import { GuidedSearchComponent } from './guided-search/guided-search.component'; 
import { QuickSearchComponent } from './quick-search/quick-search.component'; 

const searchesRoutes: Routes = [ 
    { 
     children: [ 
      { path: 'advanced-search', component: AdvancedSearchComponent }, 
      { path: 'guided-search', component: GuidedSearchComponent }, 
      { path: 'quick-search', component: QuickSearchComponent } 
     ], 
     path: '', 
     component: GuidedSearchComponent 
    } 
]; 

export const searchesRouting: ModuleWithProviders = RouterModule.forChild(searchesRoutes); 

To jest mój plik app.routing.ts:

import { ModuleWithProviders } from '@angular/core'; 
import { Routes, RouterModule } from '@angular/router'; 

const searchesRoutes: Routes = [ 
    { path: 'searches', loadChildren: 'app/searches/searches.module#SearchesModule' }, 
    { path: '', redirectTo: "/searches", pathMatch: 'full' } 
]; 

const appRoutes: Routes = [ 
    ...searchesRoutes 
]; 

export const appRoutingProviders: any[] = [ 
]; 

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes); 

To znowu nie działa poprawnie, ale jeśli wykonuję wszystkie trasy w pliku głównym, tak:

import { ModuleWithProviders } from '@angular/core'; 
import { Routes, RouterModule } from '@angular/router'; 

import { AdvancedSearchComponent } from './searches/advanced-search/advanced-search.component'; 
import { GuidedSearchComponent } from './searches/guided-search/guided-search.component'; 
import { QuickSearchComponent } from './searches/quick-search/quick-search.component'; 

const appRoutes: Routes = [ 
    { path: 'searches/advanced-search', component: AdvancedSearchComponent }, 
    { path: 'searches/guided-search', component: GuidedSearchComponent }, 
    { path: 'searches/quick-search', component: QuickSearchComponent }, 
    { path: '', redirectTo: "/searches/guided-search", pathMatch: 'full' } 
]; 

export const appRoutingProviders: any[] = [ 
]; 

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes); 

Ale chcę się nauczyć, jak przekazywać trasy wyszukiwania do modułu częściowego, aby było łatwiejsze w zarządzaniu.

Odpowiedz

4

Trzeba składnik główny w searchesRoutes dla dzieci tras jak:

@Component({ 
    selector: 'art-search', 
    template: `<router-outlet></router-outlet>` 
}) 
export class SearchComponent {} 

I trzeba także określić domyślną trasę jak:

{ path: '', redirectTo: '/guided-search', pathMatch: 'full' } 

wówczas searches.routing.ts Plik będzie:

const searchesRoutes: Routes = [ 
    { 
    children: [ 
     { path: 'advanced-search', component: AdvancedSearchComponent }, 
     { path: 'guided-search', component: GuidedSearchComponent }, 
     { path: 'quick-search', component: QuickSearchComponent }, 
     { path: '', redirectTo: '/guided-search', pathMatch: 'full' } 
    ], 
    path: '', component: SearchComponent 
    } 
]; 

Zobacz więcej szczegółów tutaj Plunker

Powiązane problemy