2017-01-19 3 views
0

Поскольку я обновил свои угловые 2 пакета (до версии 2.4.3), я обнаружил, что redirectTo больше не работает.Обновление до Angular 2.4.3: маршрутизация redirectTo больше не работает

Это мой app.routing.ts:

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

import { Features } from "./views/features/features.component"; 
import { Pricing } from "./views/pricing/pricing.component"; 
import { PricingFree } from "./views/pricing/free/pricing.free.component"; 
import { PricingPersonal } from "./views/pricing/personal/pricing.personal.component"; 
import { PricingMedium } from "./views/pricing/medium/pricing.medium.component"; 
import { PricingLarge } from "./views/pricing/large/pricing.large.component"; 
import { PricingEnterprise } from "./views/pricing/enterprise/pricing.enterprise.component"; 
import { AboutSite } from "./views/about-site/about-site.component"; 
import { AboutCompany } from "./views/about-company/about-company.component"; 
import { ContactUs } from "./views/contact-us/contact-us.component"; 
import { NoContent } from './views/no-content'; 

import { DataResolver } from './app.resolver'; 

const appRoutes: Routes = [ 
    { path: "features", component: Features }, 
    { path: "pricing", component: Pricing }, 
    { path: "pricing/free", component: PricingFree }, 
    { path: "pricing/personal", component: PricingPersonal }, 
    { path: "pricing/medium", component: PricingMedium }, 
    { path: "pricing/large", component: PricingLarge }, 
    { path: "pricing/enterprise", component: PricingEnterprise }, 
    { path: "about-site", component: AboutSite }, 
    { path: "about-company", component: AboutCompany }, 
    { path: "contact-us", component: ContactUs }, 

    // Redirects 
    { path: "", redirectTo: "/features", pathMatch: "full" }, 
    { path: "dashboard", redirectTo: "/dashboard/home", pathMatch: "full" }, 
    { path: "docs", redirectTo: "/docs/home", pathMatch: "full" }, 

    { path: '**', component: NoContent } 
]; 

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

Например, давайте посмотрим на приборную панель, которые обычно перенаправляются на приборную панель/дома, я имел это работает даже без ведущих косой черты ("приборная панель/дома "вместо"/dashboard/home ") и даже без pathMatch =" full ".

Вот мой dashboard.routing.ts:

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

import { Dashboard } from "./dashboard.component"; 
import { DashboardHome } from "./home/dashboard.home.component"; 
import { DashboardProjects } from "./projects/dashboard.projects.component"; 
import { DashboardProjectsDetail } from "./projects/detail/dashboard.projects-detail.component"; 
import { DashboardProjectsCreate } from "./projects/create/dashboard.projects-create.component"; 
import { DashboardProjectsUpdate } from "./projects/update/dashboard.projects-update.component"; 
import { DashboardJobs } from "./jobs/dashboard.jobs.component"; 
import { DashboardJobsDetail } from "./jobs/detail/dashboard.jobs-detail.component"; 
import { DashboardReports } from "./reports/dashboard.reports.component"; 
import { DashboardOrganizationInfo } from "./organization-info/dashboard.organization-info.component"; 

import { AuthGuard } from '../../helpers/auth-guard'; 

const dashboardRoutes: Routes = [ 
    { 
     path: "dashboard", component: Dashboard, canActivate: [AuthGuard], children: [ 
      { path: "home", component: DashboardHome, canActivateChild: [AuthGuard] }, 
      { path: "projects", component: DashboardProjects, canActivateChild: [AuthGuard] }, 
      { path: "projects/create", component: DashboardProjectsCreate, canActivateChild: [AuthGuard] }, 
      { path: "projects/detail/:id", component: DashboardProjectsDetail, canActivateChild: [AuthGuard] }, 
      { path: "projects/update/:id", component: DashboardProjectsUpdate, canActivateChild: [AuthGuard] }, 
      { path: "projects/update/:id/:version", component: DashboardProjectsUpdate, canActivateChild: [AuthGuard] }, 
      { path: "jobs", component: DashboardJobs, canActivateChild: [AuthGuard] }, 
      { path: "jobs/detail/:id", component: DashboardJobsDetail, canActivateChild: [AuthGuard] }, 
      { path: "reports", component: DashboardReports, canActivateChild: [AuthGuard] }, 
      { path: "organization-info", component: DashboardOrganizationInfo, canActivateChild: [AuthGuard] }   
     ] 
    } 
]; 

export const dashboardRouting: ModuleWithProviders = RouterModule.forChild(dashboardRoutes); 

Я добавил appRouting в app.module.ts в импорте:

import { appRouting } from './app.routing'; 
import { DashboardModule } from "./views/dashboard/dashboard.module"; 

@NgModule({ 
    imports: [ DashboardModule, appRouting ] 
    ... 
    }) 

Я добавил dashboardRouting в приборной панели .module.ts:

import { dashboardRouting } from "./dashboard.routing"; 

@NgModule({ 
    imports: [ dashboardRouting ] 
    ... 
    }) 

ошибок в консоли, когда я загрузить приложение в первый раз это делает redirect с localhost: от 3000 до localhost: 3000/обладает довольно странным образом.

Я следовал за Angular 2 docs, и я не вижу различий в их коде и моем.

Это не работает, когда я нажимаю на routerLink на панели управления (<a routerLink="/dashboard">) или даже когда обновляю страницу, когда я нахожусь на localhost: 3000/dashboard url.

+0

Итак, что происходит при вводе/нажатии '/ dashboard? – echonax

+0

@echonax Он идет в/dashboard, где до этого он попал в/dashboard/home –

ответ

2

Изменить перенаправления в app.routing на верхний уровень дочерних маршрутов (т. Е. Вместо /dashboard/home перенаправить на /dashboard). Затем в дочерние маршруты добавить пустой маршрут перенаправление на маршрут по умолчанию (то есть добавить маршрут для чистого ребенка /home)

См ниже код (сделать подобные изменения дляdocs):

app.routing маршруты

const appRoutes: Routes = [ 
    { path: "features", component: Features }, 
    { path: "pricing", component: Pricing }, 
    { path: "pricing/free", component: PricingFree }, 
    { path: "pricing/personal", component: PricingPersonal }, 
    { path: "pricing/medium", component: PricingMedium }, 
    { path: "pricing/large", component: PricingLarge }, 
    { path: "pricing/enterprise", component: PricingEnterprise }, 
    { path: "about-site", component: AboutSite }, 
    { path: "about-company", component: AboutCompany }, 
    { path: "contact-us", component: ContactUs }, 

    // Redirects 
    { path: "", redirectTo: "/features", pathMatch: "full" }, 

    // ==>> Redirect to '/dashboard' instead of '/dashboard/home' 
    { path: "dashboard", redirectTo: "/dashboard", pathMatch: "full" }, 

    // ==>> Redirect to '/docs' instead of '/docs/home' 
    { path: "docs", redirectTo: "/docs", pathMatch: "full" }, 

    { path: '**', component: NoContent } 
]; 

dashboard.routing маршруты

const dashboardRoutes: Routes = [ 
    { 
     path: "dashboard", component: Dashboard, canActivate: [AuthGuard], children: [ 
      // ==>> Add a blank child route redirect to '/home' child 
      { path: "", redirectTo: "home", pathMatch: "full" }, 
      { path: "home", component: DashboardHome, canActivateChild: [AuthGuard] }, 
      { path: "projects", component: DashboardProjects, canActivateChild: [AuthGuard] }, 
      { path: "projects/create", component: DashboardProjectsCreate, canActivateChild: [AuthGuard] }, 
      { path: "projects/detail/:id", component: DashboardProjectsDetail, canActivateChild: [AuthGuard] }, 
      { path: "projects/update/:id", component: DashboardProjectsUpdate, canActivateChild: [AuthGuard] }, 
      { path: "projects/update/:id/:version", component: DashboardProjectsUpdate, canActivateChild: [AuthGuard] }, 
      { path: "jobs", component: DashboardJobs, canActivateChild: [AuthGuard] }, 
      { path: "jobs/detail/:id", component: DashboardJobsDetail, canActivateChild: [AuthGuard] }, 
      { path: "reports", component: DashboardReports, canActivateChild: [AuthGuard] }, 
      { path: "organization-info", component: DashboardOrganizationInfo, canActivateChild: [AuthGuard] }   
     ] 
    } 
]; 
+0

Хорошо, это работает, НО в дочерних маршрутах панели мониторингаRouting/home не должно иметь ведущей косой черты. Он должен быть {path: "", redirectTo: "home", pathMatch: "full"}, а затем он работает, иначе он перенаправляет на localhost: 3000/home. Большое вам спасибо за этот ответ. Если вы отредактируете свой ответ, я пометю его как ответ. –

+0

@BrechtBaekelandt Вы правы. Я редактировал свой пост. –

Смежные вопросы