nagy.zsolt.hun
nagy.zsolt.hun

Reputation: 6694

Vue - watch url without VueRouter

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) }
}

Upvotes: 12

Views: 17167

Answers (1)

retrograde
retrograde

Reputation: 2979

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];
  }
}

Upvotes: 10

Related Questions