第一步
App.vue
<template> <div id="app"> <router-view v-if="isRouterAlive"/> <!-- 重点 --> </div> </template> <script> export default { name: 'app', data() { return { isRouterAlive: true } }, provide() { return { reload: this.reload } }, methods: { reload() { /**刷新当前路由**/ let self = this; self.isRouterAlive = false; self.$nextTick(function () { self.isRouterAlive = true; }); }, } } </script>
第二步
User.vue(要刷新的页面)
<template> </template> <script> export default { name: "User", inject: ['reload'], /**引入App.vue中的provide对应的方法*/ mounted() { let self = this; setTimeout(() => { console.log("刷新"); self.reload(); }, 5000); } } </script> <style scoped> </style>