Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

vue刷新组件

作者:枫尘于往逝   发布日期:2026-04-02   浏览:74

<template>
  <div>
    <button @click="refreshComponent">刷新组件</button>
    <component :is="currentComponent" />
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentComponent: 'MyComponent',
      key: 0
    };
  },
  methods: {
    refreshComponent() {
      this.key += 1;
    }
  },
  computed: {
    ComponentToRender() {
      return () => import(`@/components/${this.currentComponent}.vue`);
    }
  }
};
</script>

解释说明:

  1. 模板部分

    • 一个按钮用于触发组件刷新。
    • 使用 <component :is="currentComponent" /> 动态渲染组件。
  2. 脚本部分

    • data 中定义了 currentComponentkeycurrentComponent 是要渲染的组件名称,key 用于强制刷新组件。
    • methods 中定义了 refreshComponent 方法,每次点击按钮时增加 key 的值。
    • computed 中定义了 ComponentToRender,使用动态导入 (import) 来加载组件。
  3. 刷新机制

    • 每次点击按钮时,key 增加,Vue 会重新创建组件实例,从而实现组件的刷新。

如果需要更复杂的刷新逻辑或不同的刷新方式,可以根据具体需求调整代码。

上一篇:vue引用js文件

下一篇:vue字符串拼接

大家都在看

vue.js devtools用法

three.js vue

vue js for循环

vue.min.js 本地引入

highlight.js vue

vue.config.js 配置

vue.config.js 配置代理

vue.config.js configu

node.js vue

vue3 写法

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站