当前位置:首页 > 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 监听数据变化

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

<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 集中管理应用状态,组件可以响应状态变化实现局部更新。

// 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 值强制组件重新渲染,适用于需要完全重置组件状态的场景。

vue实现部分刷新

<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中实现无限滚动列表通常结合监听滚动事件和动态加载数据。以下是几种常见方法: 使用自定义指令 创建自定义指令v-infinite-scroll监听滚动事件: Vue.…

vue实现按钮组轮换

vue实现按钮组轮换

实现按钮组轮换的方法 在Vue中实现按钮组轮换效果可以通过动态绑定类和事件处理来完成。以下是一种常见的实现方式: 模板部分 <template> <div class="bu…

vue实现详情

vue实现详情

Vue 实现详情页的常见方法 使用动态路由 在 Vue Router 中配置动态路由参数,通过 $route.params 获取详情 ID。示例路由配置: { path: '/detail/:i…

vue实现frame

vue实现frame

Vue 中实现 iframe 的方法 在 Vue 中可以通过直接使用 <iframe> 标签或动态绑定 src 属性来实现 iframe 功能。 基本用法 <template&g…

vue实现 hover

vue实现 hover

实现 Vue 中的 hover 效果 在 Vue 中实现 hover 效果可以通过多种方式完成,以下是常见的几种方法: 使用 CSS 伪类 最简单的方式是直接使用 CSS 的 :hover 伪类,无…

vue实现答题

vue实现答题

Vue 实现答题功能 数据结构设计 使用 Vue 的响应式特性管理题目和答案数据。通常需要以下数据结构: data() { return { questions: [ {…