11

I have a method where need to create a string which consists of base URL /get-it and some random generated string?

methods:{
GenerateURL(){
...
}
}

How can be it achieved? How can I either get base URL or /get-it with vue router but without navigating to that /get-it page which is empty? I just need to use it as a string inside the component.

4 Answers 4

14

From the docs:

  • $route.path

    • type: string

      A string that equals the path of the current route, always resolved as an absolute path. e.g. "/foo/bar"

So in your case you could do something like this:

methods:{
  GenerateURL(){
    var fullUrl = window.location.origin + this.$route.path + "/get-it/yourRandomString"
  }
}

Location Origin.

Sign up to request clarification or add additional context in comments.

Comments

9

Simple and easy way to get baseUrl in VueJs is

var baseUrl = window.location.origin

1 Comment

Doesn't work because it is assuming the path is /
2

Given https://mywebsite.com/some/vue/route

import router from "@/router";

console.log(window.location.origin) // https://mywebsite.com
console.log(router.currentRoute.value.fullPath) // /some/vue/route

Comments

0

This is so simple

// app.js
Vue.prototype.$url= window.location.origin;

// in component
console.log(this.$url)

output: https://stackoverflow.com

1 Comment

Doesn't work because it is assuming the path is /

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.