業務場景:在管理后台,在執行完,增,刪,改,操作的時候。我們需要刷新一下頁面,重載數據。在JQ中我們會用到location.reload()方法,刷新頁面;在vue中,這里需要用到一個 provide / inject 這對用例。(其他方法:this.$router.go(0),會強制刷新,出現空白頁面體驗不好);
原理是:通過控制router-view 的顯示與隱藏,來重渲染路由區域,重而達到頁面刷新的效果。
代碼如下:
1:在父組件中加入
<template> <div id="app"> <router-view v-if="isRouterAlive"></router-view> </div> </template> <script> export default { name: 'App', provide (){ return { reload:this.reload } }, data(){ return { isRouterAlive:true } }, methods:{ reload (){ this.isRouterAlive = false this.$nextTick(function(){ this.isRouterAlive = true }) } }, components:{ } } </script>
2:然后在子組件export default中引入
inject:['reload']
3:最后在執行完相應的操作,后調用reload方法就可以了!
this.reload();