Skip to content

[next] active links: allow trailing slash in route matching (nested routes) #628

@jwahdatehagh

Description

@jwahdatehagh

I would like to have nested routes with a default component in the nested-root like so:

routes: [
  { 
    path: '/home', 
    name: 'home',
    component: Home,
    children: [
      {
        path: '',
        name: 'home.index',
        component: HomeIndex
      },
      {
        path: 'foo',
        name: 'home.foo',
        component: HomeFoo
      }
    ]
  }
]

For the active link highlights i'd like to scope the children separately. Can we serve the same content on both /home and /home/ and have the /home link active in both cases?

Vue.js & vue-router.js version

2.0.0-rc.4, 2.0.0-rc.4

Reproduction Link

https://jsfiddle.net/jwahdatehagh/44ed5xxf/1/

Activity

LinusBorg

LinusBorg commented on Aug 31, 2016

@LinusBorg
Member

I think this should be working as you describe. Currently it doesnt' because with do a simple === match of the path strings here:

https://github.com/vuejs/vue-router/blob/next/src/util/route.js#L8

We could maybe solve this with:

return (
      a.path.replace(/\/$/,'') === b.path.replace(/\/$/,'') &&
      a.hash === b.hash &&
      isObjectEqual(a.query, b.query)
    )

@fnlctrl What are your thoughts?

jwahdatehagh

jwahdatehagh commented on Aug 31, 2016

@jwahdatehagh
Author

Does the same problem not apply to a.hash === b.hash? Sorry, i'm not quite acquainted with the internals of the router.

LinusBorg

LinusBorg commented on Aug 31, 2016

@LinusBorg
Member

In what way? hashes don't have this issue with optional trailing slashes. hashes come after the path and don't have any slashes:

http://domain.tld/some/path/#hash?query=params

jwahdatehagh

jwahdatehagh commented on Aug 31, 2016

@jwahdatehagh
Author

I thought this check is for when the router uses the hash vs the history api e.g. domain.tld#/home/foo.

Then the same issue would occur for #/home vs #/home/.

LinusBorg

LinusBorg commented on Aug 31, 2016

@LinusBorg
Member

The hash is removed before route matching:

https://github.com/vuejs/vue-router/blob/next/src/history/hash.js#L44

(the getHash() function does it...)

So when the code reaches the section discussed previously, any hash present is a "real" hash, not a "fake path" hash.

jwahdatehagh

jwahdatehagh commented on Aug 31, 2016

@jwahdatehagh
Author

Ok, cool! Thanks for the explanation!

fnlctrl

fnlctrl commented on Sep 1, 2016

@fnlctrl
Member

@LinusBorg Though unlikely, a.path.replace(/\/$/,'') === b.path.replace(/\/$/,'') will be true for '/foo/bar' and '/foobar'.

I don't quite get what OP wants... What's wrong with adding exact on roter-link?

reopened this on Sep 1, 2016
LinusBorg

LinusBorg commented on Sep 1, 2016

@LinusBorg
Member

@fnlctrl

  1. my Regex will only remove trailing slashes, so your example should not be possible.
  2. The problem is that a router-link with a path of /home and the exact attribute will not get the activeClass set if the current route's path is /home/ - even though both routes render the same components and should therefore be considered equal in my opinion.
fnlctrl

fnlctrl commented on Sep 1, 2016

@fnlctrl
Member

@LinusBorg Oops, misread that regex...sorry.
Thanks for the explanation! I'll look into it.

fnlctrl

fnlctrl commented on Sep 1, 2016

@fnlctrl
Member

@LinusBorg I agree with removing trailing slashes before comparison.

12 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @LinusBorg@jwahdatehagh@fnlctrl

      Issue actions

        [next] active links: allow trailing slash in route matching (nested routes) · Issue #628 · vuejs/vue-router