Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router: Using UrlMatcher function throws error that path is not defined #12972

Closed
bontscho opened this issue Nov 18, 2016 · 7 comments
Closed

Comments

@bontscho
Copy link

bontscho commented Nov 18, 2016

I'm submitting a ... (check one with "x")

[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

When using the matcher function in Routes, it throws an error that no path has been defined.

Sample Config according to source code:

function matcherFunction(url: UrlSegment[]) {
     if (url.length == 1 && url[0].path.startsWith('sample-url')) {
         return {consumed: url};
     }
     return null;
 }

const routes: Routes = [
    {
        matcher: matcherFunction,
        component: ComponentToUse
    }
];

The issue is in these 3 error messages:

https://github.com/angular/angular/blob/master/modules/@angular/router/src/config.ts#L409

These error messages should be corrected to consider an existing matcher function. When omitting these 3 errors, it works as intended.

Expected behavior

It should execute the matcher function and route to the correct component for the given url

Minimal reproduction of the problem with instructions

Just add the provided matcher function and omit the path property for any given route

plnkr: http://plnkr.co/edit/vtpDnJQ4YGtuRAryknqo?p=preview

What is the motivation / use case for changing the behavior?

My usecase is to have a matcher for a route with an optional id so the routes:
/news-articles
and
/news-articles/:id

are one route so the component does not get initialised again when navigating from /news-articles to /news-articles/:id

Please tell us about your environment:
Mac OS Sierra 10.12.1, VScode 1.7.1

  • Angular version: 2.2.2
    Router version 3.2.2

  • Browser: all

  • Language: Typescript

  • Node (for AoT issues): node --version = 6.9.1

@skreborn
Copy link
Contributor

@bontscho Please add a plunker that reproduces your problem. The team has no capacity to reproduce each and every issue on their own based on instructions.

@bontscho
Copy link
Author

i added the plnkr, hope this helps

@DzmitryShylovich
Copy link
Contributor

How matcher function is a replacement for path?

@bontscho
Copy link
Author

here you have the corresponding error code when you use both at the same time:
https://github.com/angular/angular/blob/master/modules/%40angular/router/src/config.ts#L400

matcher is your custom function to define the path your route should match, here you can have optional parameters just like described in my use case

@ronaksharma8
Copy link

@bontscho i have replicated your code into demo project... I am unable to hit the component even though i pass url starting with "sample-url". Can you please let me know if i am missing something.

Below is my code for routing

export const APP_ROUTES: Routes = [
{ path: '', redirectTo: 'profile/:name', pathMatch: 'full' },
{ path: 'auth', component: AuthComponent },
{ path: 'profile/:name', component: ProfileComponent },
{ path: ':name', component: HomeComponent },
{
path: ':name/:reponame', component: ProjectComponent,
children: [
{ path: 'file', component: ProjectContentComponent }
]
},
{ path: '**', component: PageNotFoundComponent },
{
matcher: matcherFunction,
component: ProjectContentComponent
}
];

export function matcherFunction(url: UrlSegment[]) {
if (url.length == 1 && url[0].path.startsWith('sample-url')) {
return { consumed: url };
}
return null;
}

@bontscho
Copy link
Author

@ronaksharma8 the ** route should be the last, since it will always return a match (404). your last route with the matcherFunction therefore will never be checked by the router. try putting the matcher function route before the ** route

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants