Search code examples
angularionic-frameworkionic4angular-routing

Exception (special exclusion) in route parameters


I have the following route definition in my app-routing.module.ts:

{ path: 'messages/:id', loadChildren: './pages/message/message.module#MessagePageModule' },

However, I have a page that loads archived messages. Naturally, the path for this page would be something like messages/archived. However, this conflicts with the route that's already defined with an ID for the message page i.e. messages/:id.

How can I define an exception that if id == 'archived', it should load the page with archived messages?

Further info: the ID in messages is always numeral, i.e. in real time it would be something like messages/123.


Solution

  • Suggetsion: You should use query params for archived messages as this is type of messages not a different resource, whereas :id is a single resource.

    Solution: Change the order of archived route as below:

    In app-routing.module.ts

    { path: 'messages', loadChildren: './pages/message/message.module#MessagePageModule' }
    

    In message-routing.module.ts

    const messageRoutes: Routes = [
    {
        path: '',
        children: [
            {
                path: 'archived',
                component: ArchivedComponent
            },
            {
                path: ':id',
                component: MessageComponent
            },
            {
                path: '',
                component: MessageComponent,
                pathMatch: 'full'
            }
        ]
     }
    

    As order of the route matters explained here. This should solve your problem.