Hillcow
Hillcow

Reputation: 969

Vue: Watch pathname to detect URL changes

So when it comes to url changes everyone on Stackoverflow seems to suggest using 'onhashchange', but no one even speaks about the fact that this event is - like it name says - only triggered if you use hashes. And I don't want to use random hashes in my URL.

So I am looking for a way to watch window.location.pathname to detect any change there. But Vue seems to only be able to watch its own properties. So how do I do this? Thank you!

Upvotes: 1

Views: 5005

Answers (1)

Yaniv Peretz
Yaniv Peretz

Reputation: 1168

Could try this: (should work only on history mode, will not work on hash mode)

mouted () {
    window.addEventListener(
         'popstate', this.handleHistoryChange
         ) 
},

destroyed () {
     window.removeEventListener(
            'popstate', this.handleHistoryChange
        ) 
   }

Upvotes: 2

Related Questions