当前位置:首页 > VUE

vue实现表格刷新

2026-01-16 22:17:44VUE

Vue 实现表格刷新的方法

使用 v-if 强制重新渲染

通过 v-if 控制表格的显示与隐藏,切换时 Vue 会重新渲染组件。

<template>
  <button @click="refreshTable">刷新表格</button>
  <table v-if="showTable">
    <!-- 表格内容 -->
  </table>
</template>

<script>
export default {
  data() {
    return {
      showTable: true
    }
  },
  methods: {
    refreshTable() {
      this.showTable = false
      this.$nextTick(() => {
        this.showTable = true
      })
    }
  }
}
</script>

修改 key 强制更新

为表格绑定动态 key,改变 key 值会触发组件重新渲染。

vue实现表格刷新

<template>
  <button @click="refreshTable">刷新表格</button>
  <table :key="tableKey">
    <!-- 表格内容 -->
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableKey: 0
    }
  },
  methods: {
    refreshTable() {
      this.tableKey += 1
    }
  }
}
</script>

通过数据更新驱动刷新

直接更新表格绑定的数据源,Vue 的响应式系统会自动更新视图。

vue实现表格刷新

<template>
  <button @click="refreshTable">刷新表格</button>
  <table>
    <tr v-for="item in tableData" :key="item.id">
      <td>{{ item.name }}</td>
    </tr>
  </table>
</template>

<script>
export default {
  data() {
    return {
      tableData: []
    }
  },
  methods: {
    async refreshTable() {
      const res = await fetch('/api/data')
      this.tableData = await res.json()
    }
  }
}
</script>

使用 $forceUpdate 方法

强制组件重新渲染,适用于数据更新但视图未响应的情况。

<template>
  <button @click="refreshTable">刷新表格</button>
  <table>
    <!-- 表格内容 -->
  </table>
</template>

<script>
export default {
  methods: {
    refreshTable() {
      this.$forceUpdate()
    }
  }
}
</script>

结合 Vuex 状态管理

通过 Vuex 管理表格数据,提交 mutation 或 action 更新数据。

// store.js
export default new Vuex.Store({
  state: {
    tableData: []
  },
  mutations: {
    updateTableData(state, payload) {
      state.tableData = payload
    }
  },
  actions: {
    async fetchTableData({ commit }) {
      const res = await fetch('/api/data')
      commit('updateTableData', await res.json())
    }
  }
})
<template>
  <button @click="refreshTable">刷新表格</button>
  <table>
    <tr v-for="item in $store.state.tableData" :key="item.id">
      <td>{{ item.name }}</td>
    </tr>
  </table>
</template>

<script>
export default {
  methods: {
    refreshTable() {
      this.$store.dispatch('fetchTableData')
    }
  }
}
</script>

注意事项

  • 数据驱动更新是 Vue 推荐的方式,优先考虑通过修改数据实现刷新
  • v-ifkey 方法会触发组件完全重新渲染,可能影响性能
  • $forceUpdate 不会更新子组件,仅强制当前组件重新渲染
  • 大型项目建议使用 Vuex 或 Pinia 集中管理状态

标签: 表格vue
分享给朋友:

相关文章

vue实现增删改

vue实现增删改

Vue 实现增删改查功能 Vue.js 是一个流行的前端框架,用于构建用户界面。以下是使用 Vue.js 实现增删改查(CRUD)功能的方法。 数据初始化 在 Vue 实例的 data 中初始化一个…

vue穿梭框组件实现

vue穿梭框组件实现

实现基础穿梭框组件 在Vue中实现穿梭框组件,可以使用<el-transfer>(Element UI)或手动实现。以下是手动实现的核心逻辑: <template> &l…

vue 实现excel

vue 实现excel

Vue 实现 Excel 功能 前端导出 Excel 使用 xlsx 库可以轻松实现前端 Excel 导出功能。安装依赖: npm install xlsx 导出 Excel 示例代码: impo…

vue 分页 实现

vue 分页 实现

Vue 分页实现方法 使用第三方库(如 Element UI) Element UI 提供了现成的分页组件 el-pagination,适合快速集成。 安装 Element UI: npm ins…

vue 实现templateurl

vue 实现templateurl

在 Vue 中,templateUrl 是 Angular 中的一个概念,用于指定外部 HTML 文件作为模板。Vue 本身不直接支持 templateUrl,但可以通过以下方法实现类似功能: 使用…

黑马vue实现

黑马vue实现

以下是关于使用 Vue.js 实现黑马相关功能或项目的关键方法和实践指南: 环境配置与项目初始化 安装最新版 Vue CLI 或使用 Vite 创建项目: npm create vue@lates…