Is it possible to get full url (including origin) from route in VueJS?

Viewed 10140

In this example:

const resolved = this.$router.resolve({
  name: 'about'
})

console.log(resolved.route.path)

Is it possible to get route with origin included? Like, if the url is site.com/about, the code would give /about, so I'll need to append origin myself: window.location.origin + resolved.route.path.

2 Answers

I've been using this (tested with vue-router v4, browser only):

const route = router.resolve({ /* your route here */ });
const absoluteURL = new URL(route.href, window.location.origin).href;

Edit: Used location.origin instead of location.href since resolve().href already includes base.

No, not from the router.

Even the router's base property is relative to the app root:

The base URL of the app. For example, if the entire single page application is served under /app/, then base should use the value "/app/".

$route.fullPath also begins at the app root. The docs describe it as:

The full resolved URL including query and hash.

Related