0
votes

Since I've updated my angular 2 packages (to version 2.4.3) I found that the redirectTo doesn't work anymore.

This is my 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);

For example let's look into dashboard that normally redirected to dashboard/home, I had it working even without the leading slash ("dashboard/home" instead of "/dashboard/home") and even without pathMatch="full".

Here is my 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);

I've added the appRouting in app.module.ts in the imports:

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

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

I've added dashboardRouting in dashboard.module.ts:

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

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

No errors in console, when I load the app for the first time it does redirect from localhost:3000 to localhost:3000/features weirdly enough.

I followed the Angular 2 docs, and I don't see any differences in their code and mine.

It doesn't work when I click a routerLink to dashboard (<a routerLink="/dashboard">) or even when I refresh the page when I'm at the localhost:3000/dashboard url. 

1
So what is happening when you type/click '/dashboard' ?eko
@echonax It goes to /dashboard, where before it went to /dashboard/homeBrecht Baekelandt

1 Answers

2
votes

Change the redirects in app.routing to top level of the child routes (i.e. instead of /dashboard/home redirect to /dashboard). Then in the child routes add a blank route redirect to the default route (i.e. add a route for blank child to /home)

See code below (do similar changes for docs):

app.routing routes

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 routes

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] }          
        ]
    }
];