Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match route only if param is integer in Angular2?

For example this router

{
    path: '/client',
    component: ClientRootComponent,
    children: [
        {path: '', component: ClientListComponent},
        {path: ':clientId', component: ClientOpenComponent, resolve: makeResolver(ClientOpenResolver)}
    ]
},
{
    path: '**',
    component: NotFoundComponent
}

will pass both URLs /client/1234 and /client/asdf to ClientOpenComponent. How I can make that /client/asdf will be matched as NotFound and passed to NotFoundComponent?

like image 490
RNR Avatar asked Jan 09 '17 07:01

RNR


People also ask

How to pass route params in angular?

Angular Route Params: How to Pass Route Params in Angular. To access route parameters and query parameters in Angular, use ActivatedRoute service. The ActivatedRoute service provides Observables through which we can subscribe to the values of route params and route query params. In Angular, we can pass the data as route parameters in two ways.

How to pass values from one view to another in angular?

In Angular, we can pass the data as route parameters in two ways. Route params(Route parameters) Route queryParams(Route query parameters) If we want to pass values between views, then we can use route params. For example, if we’re going to pass an ID from one route to another and fetch the id on a component onInit(), then we can use route params.

What are query parameters in angular?

Query parameters in Angular allow for passing optional parameters across any route in the application. Query parameters are different from regular route parameters, which are only available on one route and are not optional (e.g., /product/:id ). In this article, you will reference an example of an application that displays a list of products.

What is the parammap in angular?

The Angular adds the map all the route parameters in the ParamMap object, which can be accessed from the ActivatedRoute service The ParamMap makes it easier to work with parameters.


1 Answers

You can pass a custom matcher to your route

import { defaultUrlMatcher } from '@angular/router/src/shared';

function digitsMatcher(segments: UrlSegment[], segmentGroup: UrlSegmentGroup, route: Route): UrlMatchResult | null {
  const result = defaultUrlMatcher(segments, segmentGroup, route);

  if (!result || !result.consumed || result.consumed.length < 1) {
    return;
  }

  const re = /^\d+$/;
  const match = re.exec(result.consumed[0].path);

  if (match) {
    return result;
  }

  return null;
}
{
    path: '/client',
    component: ClientRootComponent,
    children: [
        {path: '', component: ClientListComponent},
        {path: ':clientId', component: ClientOpenComponent, resolve: makeResolver(ClientOpenResolver), matcher: digitsMatcher}
    ]
},
{
    path: '**',
    component: NotFoundComponent
}

The code is not tested

See also

  • https://github.com/angular/angular/blob/73407351e7fa75250be8bdb6c1eb4f7d37f6f947/modules/%40angular/router/src/shared.ts#L39
  • https://angular.io/docs/ts/latest/api/router/index/Routes-type-alias.html
  • https://angular.io/docs/ts/latest/api/router/UrlMatcher
like image 51
Günter Zöchbauer Avatar answered Oct 09 '22 04:10

Günter Zöchbauer