当前位置:首页 > VUE

vue实现部分刷新

2026-03-29 21:05:41VUE

Vue 实现部分刷新的方法

使用 v-if 或 v-show 控制显示

通过条件渲染动态切换 DOM 元素的显示与隐藏,避免整个页面重新加载。

<template>
  <div>
    <button @click="toggleContent">切换内容</button>
    <div v-if="showContent">需要刷新的内容</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showContent: false
    }
  },
  methods: {
    toggleContent() {
      this.showContent = !this.showContent
    }
  }
}
</script>

利用 computed 计算属性

计算属性会根据依赖的响应式数据自动更新,适合处理需要动态计算的数据。

<template>
  <div>
    <input v-model="inputValue" />
    <p>{{ computedValue }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      inputValue: ''
    }
  },
  computed: {
    computedValue() {
      return this.inputValue.toUpperCase()
    }
  }
}
</script>

使用 watch 监听数据变化

通过监听特定数据的变化,执行相应操作实现局部更新。

vue实现部分刷新

<template>
  <div>
    <input v-model="searchQuery" />
    <div>{{ searchResults }}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      searchResults: ''
    }
  },
  watch: {
    searchQuery(newVal) {
      this.searchResults = `正在搜索: ${newVal}`
    }
  }
}
</script>

动态组件切换

使用 <component :is="currentComponent"> 实现不同组件的动态加载和切换。

<template>
  <div>
    <button @click="currentComponent = 'ComponentA'">组件A</button>
    <button @click="currentComponent = 'ComponentB'">组件B</button>
    <component :is="currentComponent" />
  </div>
</template>

<script>
import ComponentA from './ComponentA.vue'
import ComponentB from './ComponentB.vue'

export default {
  components: { ComponentA, ComponentB },
  data() {
    return {
      currentComponent: 'ComponentA'
    }
  }
}
</script>

使用 Vuex 管理状态

通过 Vuex 集中管理应用状态,组件可以响应状态变化实现局部更新。

vue实现部分刷新

// store.js
import { createStore } from 'vuex'

export default createStore({
  state: {
    count: 0
  },
  mutations: {
    increment(state) {
      state.count++
    }
  }
})
<!-- 组件中使用 -->
<template>
  <div>
    <p>{{ $store.state.count }}</p>
    <button @click="$store.commit('increment')">增加</button>
  </div>
</template>

通过 $forceUpdate 强制更新

在极少数需要强制重新渲染组件的情况下使用,不推荐常规使用。

<template>
  <div>
    <p>{{ message }}</p>
    <button @click="updateMessage">更新消息</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      message: '原始消息'
    }
  },
  methods: {
    updateMessage() {
      this.message = '新消息'
      this.$forceUpdate()
    }
  }
}
</script>

使用 key 属性强制重新渲染

通过改变 key 值强制组件重新渲染,适用于需要完全重置组件状态的场景。

<template>
  <div>
    <ChildComponent :key="componentKey" />
    <button @click="refreshComponent">刷新组件</button>
  </div>
</template>

<script>
import ChildComponent from './ChildComponent.vue'

export default {
  components: { ChildComponent },
  data() {
    return {
      componentKey: 0
    }
  },
  methods: {
    refreshComponent() {
      this.componentKey += 1
    }
  }
}
</script>

标签: 部分vue
分享给朋友:

相关文章

vue登录逻辑的实现

vue登录逻辑的实现

Vue 登录逻辑实现 前端实现 创建登录组件 在 Vue 项目中创建一个登录组件,通常命名为 Login.vue。该组件包含表单元素,如用户名和密码输入框,以及提交按钮。 <template&…

vue实现checkbox

vue实现checkbox

Vue 实现 Checkbox 在 Vue 中实现 Checkbox 可以通过原生 HTML 的 <input type="checkbox"> 或使用 Vue 的 v-model 指令绑…

vue rooter 实现原理

vue rooter 实现原理

Vue Router 实现原理 Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。其核心实现原理包括路由匹配、组件渲染和导航守卫等机制。 路由匹配与动态路由 V…

vue实现登录认证

vue实现登录认证

Vue 登录认证实现方法 使用 Vue Router 和 Vuex 管理登录状态 在 Vue 项目中,通常使用 Vue Router 进行路由管理,Vuex 进行状态管理。登录认证的核心是验证用户身份…

vue实现点击下载

vue实现点击下载

使用 <a> 标签实现下载 在 Vue 中可以通过动态生成 <a> 标签实现文件下载。适用于已知文件 URL 的场景。 <template> <butt…

vue实现特效轮播

vue实现特效轮播

Vue实现特效轮播的方法 使用Vue实现轮播效果可以通过多种方式完成,以下是几种常见的实现方法。 使用Vue和CSS动画 通过Vue的动态绑定和CSS动画结合,可以实现平滑的轮播效果。 <t…