Reputation:
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.
Upvotes: 11
Views: 37231
Reputation: 711
This is so simple
// app.js
Vue.prototype.$url= window.location.origin;
// in component
console.log(this.$url)
output: https://stackoverflow.com
Upvotes: 0
Reputation: 5993
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
Upvotes: 2
Reputation: 1668
Simple and easy way to get baseUrl in VueJs is
var baseUrl = window.location.origin
Upvotes: 9
Reputation: 4163
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"
}
}
Upvotes: 14