How to get baseURL inside Vuejs method using vue router?

11,926

Solution 1

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.

Solution 2

Simple and easy way to get baseUrl in VueJs is

var baseUrl = window.location.origin

Solution 3

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
Share:
11,926
Admin
Author by

Admin

Updated on June 23, 2022

Comments

  • Admin
    Admin almost 2 years

    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.