Vue中刷新页面的三种方式

时间:2025-03-01 07:36:22

一、通过js原始方法刷新

缺点: 出现闪白

<template>
  <div>
      <div class="header">
          <button @click="update()">刷新页面</button>
      </div>
  </div>
</template>

<script>
export default {
data(){
  return{
  }
},
methods:{
  update(){
    ()
  }
}
}
</script>

二、通过Vue自带的路由进行跳转

缺点: 出现闪白

<template>
  <div>
      <div class="header">
          <button @click="update()">刷新页面</button>
      </div>
  </div>
</template>

<script>
export default {
data(){
  return{
  }
},
methods:{
  update(){
    this.$(0)
  }
}
}
</script>

三、通过在APP页面进行demo进行刷新(推荐)

优点: 不闪白

  1. vue2写法

(1)、在APP页面中写入下面代码

<template>
  <div >
    <router-view v-if="isShow"/>
  </div>
</template>

<script>
export default {
  name: 'App',
  provide(){
    return{
      reload:
    }
  },
  data(){
    return{
      isShow:true
    }
  },
  methods:{
    reload(){
      =false;
      this.$nextTick(()=>{
        =true
      })
    }
  }
}
</script>

(2)、在需要刷新的页面进行引入并使用

<template>
  <div>
      <div class="header">
          <button @click="update()">刷新页面</button>
      </div>
  </div>
</template>

<script>
export default {
data(){
  return{

  }
},
inject:[
  'reload'
],
methods:{
  update(){
    ()
    ('刷新页面')
  }
}
}
</script>

2. vue3.2写法

(1)、在APP页面中写入下面代码

<template>
    <router-view v-if="isRouter" />
</template>

<script setup>
import { nextTick, provide, ref } from "Vue"
const isRouter = ref(true)
const reload = () => {
     = false
    nextTick(() => {
         = true
    })
}
provide("reload", reload)
</script>

<style scoped>
</style>

(2)、在需要刷新的页面进行引入并使用

<script setup>
import { inject } from 'vue'
const reload = inject("reload")
// 刷新页面
const onSubmitForm = () => {
    reload()
}
</script>

如果对您有用的话,别忘了给个三连,多谢多谢

相关文章