简体   繁体   English

如何使用 onclick 方法更新 vue 插值?

[英]How do you update a vue interpolation using an onclick method?

I am struggling to update the interpolation of thing .我正在努力更新thing的插值。 This simple function should change the value of the data but instead does nothing.这个简单的函数应该改变数据的值,但什么也不做。 Chrome logs the change of thing in the console but the HTML does not update. Chrome 在控制台中记录了事物的变化,但 HTML 没有更新。

<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);
      return this.thing;
    },
  },
});
</script>

Try to update your data property instead of returning:尝试更新您的数据属性而不是返回:

dropSelect(thing: any) {
  this.thing = thing
},

One small observation : Use this.thing = thing instead of return this.thing一个小观察使用this.thing = thing而不是 return this.thing

Demo :演示

 new Vue({ el: '#app', data: { thing: "A" }, methods: { dropSelect(thing) { this.thing = thing; } } })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <button @click="dropSelect('B')"> {{ thing }} </button> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM