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.