JavaScript/vue.js/刷新页面:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
创建页面,内容为“== 无需重新加载页面或刷新dom == === history.pushState === <syntaxhighlight lang="javascript"> addHashToLocation(params) { history.pushState( {},…”
 
Kangkk​(留言 | 贡献)
 
(未显示同一用户的1个中间版本)
第29行: 第29行:
this.$nuxt.$emit('pushState', params)</syntaxhighlight>
this.$nuxt.$emit('pushState', params)</syntaxhighlight>
=== history.replaceState ===
=== history.replaceState ===
== 点击页面刷新之前触发事件 ==
=== onbeforeunload ===
事件在即将离开当前页面(刷新或关闭)时触发。
=== onunload ===
=== 导航守卫 ===
导航守卫主要用来通过跳转或取消的方式守卫导航。有多种机会植入路由导航过程中:全局的, 单个路由独享的, 或者组件级的。
* https://router.vuejs.org/zh/guide/advanced/navigation-guards.html#全局前置守卫

2020年7月23日 (四) 11:08的最新版本

无需重新加载页面或刷新dom

history.pushState

addHashToLocation(params) {
  history.pushState(
    {},
    null,
    this.$route.path + '#' + encodeURIComponent(params)
  )
}
addHashToLocation('/my/new/path')
Warning: Vue Router不知道url已更改,因此在pushState之后它不会反映url
// main component
created() {
  // event fire when pushState
  this.$nuxt.$on('pushState', params => {
    // do your logic with params
  })
},
beforeDestroy() {
  this.$nuxt.$off('pushState')
},
...

// Where there are history.pushState
this.$nuxt.$emit('pushState', params)

history.replaceState

点击页面刷新之前触发事件

onbeforeunload

事件在即将离开当前页面(刷新或关闭)时触发。

onunload

导航守卫

导航守卫主要用来通过跳转或取消的方式守卫导航。有多种机会植入路由导航过程中:全局的, 单个路由独享的, 或者组件级的。