前些日子項目中忽然接到了一個需求,要求點擊當前路由刷新頁面,進過實驗有以下幾種方案可實現需求,並簡述不一樣。html
此方式是利用了 history 中前進和後退的功能,傳入 0 刷新當前頁面。
缺點:頁面整個刷新,會白屏。this
直接使用刷新當前頁面的方法。
缺點:同 this.$router.go(0)
同樣,會白屏。url
經過 $nextTick()
,協助實現。先把 <router-view />
移除,移除後再從新添加,達到刷新當前頁面的功能。是目前最合適的實現方式。code
<!-- html --> <router-link :to="url" @click.native="refreshView">頁面</router-link> <router-view v-if="showView"/> <!-- js --> <script> export default { data () { return { showView: true // 用於點擊當前頁的router時,刷新當前頁 } }, methods: { refreshView () { this.showView = false // 經過v-if移除router-view節點 this.$nextTick(() => { this.showView = true // DOM更新後再經過v-if添加router-view節點 }) } } } </script>