首页
归档
笔记
树洞
搜索
友言

文章详情

Interesting People Record Interesting.

/ JavaScript / 文章详情

vue项目如何刷新当前页面?

Sonder
2020-09-21
760字
2分钟
浏览 (2.3k)

切换导航菜单时,希望获取新数据。
方式有很多种,今天我就介绍最实用最简单的吧,让大家少走点弯路。

首先,要修改下你的app.vue

复制代码
<template>
 <div id="app">
   <router-view v-if="isRouterAlive"/>
 </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
     })
   }
 }
}

app.vue里写好reload()方法注入你需要的场景里:

如导航A菜单组件: A.vue

复制代码
export default {
 inject: ['reload'],
 watch: {
   $route (to, from) {
     if (to.path === '/path/A.vue') {
       this.reload()
     }
   }
 },
}

搞定了!

下一篇 / Vue项目打包压缩:让页面更快响应

🎯 相关文章

💡 推荐文章

🕵️‍♂️ 评论 (0)