Vue - 在没有 VueRouter 的情况下观看 url

nag*_*hun 6 url watch vue.js

是否可以在不包含 VueRouter 的情况下对 Vue 组件中的 URL 更改做出反应?

如果存在 VueRouter,我们可以查看 $route,但是,我的应用程序不依赖于 VueRouter。

export default {
    watch: { '$route': route => console.log(window.location.href) }
}
Run Code Online (Sandbox Code Playgroud)

ret*_*ade 7

在我使用 vue router 之前,我做了这样的事情......

data: function() {
  return {
     route: window.location.hash,
     page: 'home'
  }
},
watch: {
  route: function(){
    this.page = window.location.hash.split("#/")[1];
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 有一个 [hashchange](https://developer.mozilla.org/en-US/docs/Web/Events/hashchange) 事件。海报似乎想看`href`。 (4认同)