If you are interested ONLY on relative paths like: /dashboard
, /about
etc, See other answers.
If you want to open an absolute path like: https://www.google.com
to a new tab, you have to know that Vue Router is NOT meant to handle those.
However, they seems to consider that as a feature-request. #1280. But until they do that,
Here is a little trick you can do to handle external links with vue-router.
- Go to the router configuration (probably
router.js
) and add this code:
/* Vue Router is not meant to handle absolute urls. */
/* So whenever we want to deal with those, we can use this.$router.absUrl(url) */
Router.prototype.absUrl = function(url, newTab = true) {
const link = document.createElement('a')
link.href = url
link.target = newTab ? '_blank' : ''
if (newTab) link.rel = 'noopener noreferrer' // IMPORTANT to add this
link.click()
}
Now, whenever we deal with absolute URLs we have a solution. For example to open google to a new tab
this.$router.absUrl('https://www.google.com)
Remember that whenever we open another page to a new tab we MUST use noopener noreferrer
.
Read more here
or Here
target="_blank"
to inside my router-link tag. but i am not sure if this is good practice or not. and I don't have any param – Chaplin