'What is the best way to grab first URL segment from Vue.js hash mode?

I have this URL generated by my vue.js app

http://localhost:8080/#/url-groups

I need to access url-groups

I've tried

const firstSegment = new URL(window.location.href).pathname.split('/#/')

I kept getting '/'


I've also tried :

const firstSegment = new URL(window.location.href).pathname.split('#'); console.log(firstSegment);
VM21687:1 ['/']


const firstSegment = new URL(window.location.href).pathname.split('/#/'); console.log(firstSegment);
VM21719:1 ['/']


const firstSegment = new URL(window.location.href).pathname.split('/'); console.log(firstSegment);
VM21751:1 (2) ['', '']

Can someone pls correct me ?



Solution 1:[1]

Try out to get the hash segment then split using / as separator finally get the element of the splitted array :

const [,secondSegment] = new URL(window.location.href).hash.split('/')

or

const segment= new URL(window.location.href).hash.replace('#/','')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1