Skip to content

RouterLink: incorrect relative link if defined in component having empty path #13011

Closed

Description

@dubadub

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
In the case of such router config with ContainerComponent consuming empty path:

const routes = [
  { 
    path: '', 
    component: ContainerComponent,
    children: [
      { path: 'a', component: AComponent },
      { path: 'b', component: BComponent },
    ]
  }
];

and ContainerComponent having relative links to children in template:

<a [routerLink]="['a']">Link to A</a>     (or  <a [routerLink]="['./a']">Link to A</a>)
<a [routerLink]="['b']">Link to B</a>

RouterLink directive generates incorrect links when user navigated to one of the child component. F.e. if user went to /a route, links would be like that: /a/(a) and /a/(b).

Expected behavior

Relative links should be generated properly: /a and /b.

Minimal reproduction of the problem with instructions
http://plnkr.co/edit/Lf2Vfe?p=preview
Click on any link. Then you could see that links rendered in AppComponent are correct, but not the ones in ContainerComponent (they both look like /a/(a) and /a/(b)).

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

Please tell us about your environment:

  • Angular version: 2.2.1
  • Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]
  • Language: [all | TypeScript X.X | ES6/7 | ES5]

  • Node (for AoT issues): 4.2.1

Activity

  1. DzmitryShylovich commented on Nov 22, 2016

    @DzmitryShylovich
    Contributor
  2. dubadub commented on Nov 22, 2016

    @dubadub
    Author

    @DzmitryShylovich Yeah, absolute paths work just fine. Problem comes when used relative path. It's necessary to use relative path for example in case of reusable module with own navigation bar and which is connected to components tree on quite deep level. It's not very handy to provide variable with path from the root.

  3. dubadub commented on Nov 22, 2016

    @dubadub
    Author

    It seems to me that I did everything right according to docs. Maybe I missed something. But... @DzmitryShylovich Did you manage to see that links look like /a/(a) and /a/(b)

  4. wKoza commented on Nov 22, 2016

    @wKoza
    Contributor
  5. buctwbzs commented on Nov 24, 2016

    @buctwbzs

    i think you maybe missed something.read docs,you will get it .

  6. vicb commented on Dec 1, 2016

    @vicb
    Contributor

    sounds like a legit bug

  7. klinki commented on Jan 12, 2017

    @klinki

    I have the same problem. It still doesn't work properly in templates.

  8. leon commented on Jan 23, 2017

    @leon

    I have the same problem.
    It looks like the router doesn't resolve urls which either don't have a path, or are abstract paths (no component).

    export const ROUTES = [
      {
        // Index route job or product admin selection
        path: '',
        component: ProductsRoute
      },
      {
        path: ':locale',
        resolve: { categories: CategoriesResolver },
        children: [
          {
            // Index route for categories
            path: '',
            component: CategoriesRoute
          },
          {
            path: ':categoryId',
            resolve: { category: CategoryResolver },
            children: [
              {
                // Index route for a category
                path: '',
                component: CategoryRoute
              },
              {
                path: ':productId',
                component: ProductRoute,
                resolve: { product: ProductResolver }
              },
            ]
          },
        ]
      }
    ];
    

    When I'm in a category and try to link up one level with

    <a routerLink="../">Parent</a>
    

    I'm still getting /en/1 instead of /en

    I've iven tried doing it from the Category Component with the same result

    const urlTree = this.router.createUrlTree(['../'], { relativeTo: this.route });
    console.log('CategoryRoute URL: ', urlTree.toString()); // outputs /en/1
    
  9. furcino commented on Feb 20, 2017

    @furcino

    Hello, I have the same problem. If there is a route localhost/parent/child and I have a [routerLink]="[../sibling]" link, it creates the url localhost/parent/child/sibling instead of localhost/parent/sibling.

    The only way to get the desired behavior is to use absolute links, which is undesirable with standalone modules.

    The problem occurs only if I use a module with links to sub-modules. For example an authorization module with a login module, sing up module etc. I can not link from the sign up module to the login module without using absolute links. If I change the routing to only use the components (LoginComponent, RegisterComponent) and all these are accessible from the authorization module, the relative links work.

  10. ytilis commented on Mar 21, 2017

    @ytilis

    Can confirm that we're seeing the same issue as @leon with pathless routes breaking relative routerLink calls. Adding a path value fixes it, so it's definitely a bug. Using absolute paths for now until this is resolved.

  11. anantkamath commented on Apr 26, 2017

    @anantkamath

    I'm facing this issue too (angular 2 final release)

  12. 50 remaining items

  13. added a commit that references this issue on Jun 9, 2020
    585e3f6
  14. added a commit that references this issue on Jun 25, 2020
    2a72b91
  15. angular-automatic-lock-bot commented on Jul 10, 2020

    @angular-automatic-lock-bot

    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.

  16. locked and limited conversation to collaborators on Jul 10, 2020
  17. added a commit that references this issue on Sep 5, 2020
    2298b87
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions