question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

<router-link> not working with named route and hash

See original GitHub issue

Version

3.1.2

Reproduction link

https://codesandbox.io/s/vue-routing-example-bcy1y

Steps to reproduce

switch between pages and check the hash and the url

What is expected?

:to="{name: 'hello', hash: 'test'}" => /hello#test

What is actually happening?

:to="{name: 'hello', hash: 'test'}" => /hellotest


Hash is working fine when using path so it’s definitely not a major or blocking problem but I personally use named route whenever I can and also with nuxt-i18n and the hash is just appended without the # to the route.

Thanks for the awesome work.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
domorancommented, Jan 7, 2021

Same problem here, please reopen this issue.

<router-link :to="{ name: 'home', hash: '#contact-us' }">Contact Us</router-link> The link becomes /home#contact-us When clicked, the URL changes, but the pages doesn’t scroll.

Although I got not even an error in the console.

I found a workaround to this behaviour - if you insert this (found on the router documentation) in your router configuration

  scrollBehavior (to, from, savedPosition) {
      if (to.hash) {
         return { selector: to.hash }
      }
  }

then scrolling works as expected after page navigation. Still I think this should be a default behaviour.

7reactions
fawadahmadcommented, Jul 15, 2020

<router-link :to="{ name: 'home', hash: '#contact-us' }">Contact Us</router-link> The link becomes /home#contact-us When clicked, the URL changes, but the pages doesn’t scroll. Also there is an error in the console: Uncaught Error: Syntax error, unrecognized expression: /home#contact-us

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular2 Routing with Hashtag to page anchor - Stack Overflow
This appends the hash to the end of my url properly, but it doesn't anchor to the div with the same name. I'm...
Read more >
Different History modes - Vue Router
The history option when creating the router instance allows us to choose among different history modes. Hash Mode #. The hash history mode...
Read more >
How to Use Vue Router: A Complete Tutorial - Vue School Blog
On line 3 we are declaring an array of objects named routes. This array represents our routes in the application.
Read more >
<Link> · React Router
A <Link> can know when the route it links to is active and automatically apply an ... and will not scroll to the...
Read more >
Common Routing Tasks - Angular
The following command uses the Angular CLI to generate a basic Angular application with an application routing module, called AppRoutingModule , which is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found