Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue - watch url without VueRouter

Tags:

url

vue.js

watch

Is it possible to react on URL changes in a Vue component, without including VueRouter?

In case VueRouter is present, we can watch $route, however, my application does not rely on VueRouter.

export default {
    watch: { '$route': route => console.log(window.location.href) }
}
like image 595
nagy.zsolt.hun Avatar asked Jan 18 '18 23:01

nagy.zsolt.hun


1 Answers

Before I used vue router, I did something like this...

data: function() {
  return {
     route: window.location.hash,
     page: 'home'
  }
},
watch: {
  route: function(){
    this.page = window.location.hash.split("#/")[1];
  }
}
like image 192
retrograde Avatar answered Oct 23 '22 04:10

retrograde