You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过onclick方法更新Vue插值?点击后视图未更新问题排查

问题根源

dropSelect方法未对响应式属性this.thing执行赋值操作,仅做了参数打印和返回当前属性值的操作,data中的thing始终为初始值"A",无法触发视图更新。

修复方法

修改方法逻辑,将传入的参数赋值给this.thing即可,不需要额外返回值:

<template>
  <div class="container-xl pb-5">
    <button class="mb-2 btn-lg selected-dropdown" @click="dropSelect('B')">
      {{ thing }}
    </button>
  </div>
</template>

<script lang="ts">
import { defineComponent } from "vue";

export default defineComponent({
  data() {
    return {
      thing: "A",
    };
  },
  methods: {
    dropSelect(thing: any) {
      console.log(thing);
      this.thing = thing;
    },
  },
});
</script>

修改后点击按钮,thing的值会被更新为传入的"B",模板插值会同步刷新。

内容的提问来源于stack exchange,提问作者robinvr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.01 19:24:31