0

I have a defined route in angular2 application and receive such error:

Cannot match any routes: ''

export const routes: Routes = [{
  path: '',
  component: MainComponent,

  children: [{
    path: 'callCenter',
    component: CallCenterComponent,
    resolve: {
      countries: CountriesResolver
    },
  }, {
    path: 'vehicle',
    component: VehicleComponent,
    resolve: {
      vehicleCategories: VehicleCategoryResolver,
      vehicleOptions: VehicleOptionResolver
    },
  }, {
    path: 'territory',
    component: TerritoryComponent,
    resolve: {
      territoryOptions: TerritoryOptionResolver
    }
  },{
    path: 'token',
    component: TokenGenerationComponent
  }, {
    path: 'rider',
    component: RiderComponent
  }, {
    path: 'dispatch',
    component: DispatchSchemaComponent,
    resolve: {
      dispatchOrders: DispatchOrderResolver,
      dispatchModes: DispatchModeResolver
    }
  }]
}, {
  path: 'login',
  component: LoginComponent
}];

This is a code of my route config. I make it the same as in examples but it does not work, redirectTo property does not help also. default empty route does not loading. Could anyone help me?

Nick Shulzhenko
  • 145
  • 1
  • 10

1 Answers1

1

I believe angular is expecting MainComponent child routes to have route with path ''.

 export const routes: Routes = [{
  path: '',
  component: MainComponent,

  children: [{
      path: '',
      component: SomeComponent
     },
     {
     path: 'callCenter',
     component: CallCenterComponent,
     resolve: {
      countries: CountriesResolver
    },
  }, {
    path: 'vehicle',
    component: VehicleComponent,
    resolve: {
      vehicleCategories: VehicleCategoryResolver,
      vehicleOptions: VehicleOptionResolver
    },
  }, {
    path: 'territory',
    component: TerritoryComponent,
    resolve: {
      territoryOptions: TerritoryOptionResolver
    }
  },{
    path: 'token',
    component: TokenGenerationComponent
  }, {
    path: 'rider',
    component: RiderComponent
  }, {
    path: 'dispatch',
    component: DispatchSchemaComponent,
    resolve: {
      dispatchOrders: DispatchOrderResolver,
      dispatchModes: DispatchModeResolver
    }
  }]
}, {
  path: 'login',
  component: LoginComponent
}];

Edit: With some redirecting

export const routes: Routes = [
  {
    path: '',
    component: MainComponent,
    children: [
      {
        path: '',
        redirectTo: 'callCenter'
      },
      {
        path: 'callCenter',
        component: CallCenterComponent,
        resolve: {
          countries: CountriesResolver
        },
      }, {
        path: 'vehicle',
        component: VehicleComponent,
        resolve: {
          vehicleCategories: VehicleCategoryResolver,
          vehicleOptions: VehicleOptionResolver
        },
      }
    ...

Moving the MainComponent to child routes should also work

export const routes: Routes = [
  {
    path: '',
    children: [
      {
        path: '',
        component: MainComponent
      },
      {
        path: 'callCenter',
        component: CallCenterComponent,
        resolve: {
          countries: CountriesResolver
        },
      }, {
        path: 'vehicle',
        component: VehicleComponent,
        resolve: {
          vehicleCategories: VehicleCategoryResolver,
          vehicleOptions: VehicleOptionResolver
        },
      }
    ...
Kyrsberg
  • 440
  • 4
  • 11
  • Thank you a lot, can you please describe why it works in this way? Anf also If I do not want to use default component, can I use redirectTo here or some kind of mocked empty component? – Nick Shulzhenko Oct 26 '16 at 10:42
  • I don't know why angular does it like this, maybe someone wiser can answer that. You can redirect to another route with redirectTo, i edited answer a little – Kyrsberg Oct 26 '16 at 11:57
  • Thank you a lot for a help, you save a lot of time for me, because this route is very different from usual ui-router from the first angular that I used to. – Nick Shulzhenko Oct 26 '16 at 14:39