【问题标题】:Methods in Components in Vue jsVue js 组件中的方法
【发布时间】:2019-03-16 22:37:20
【问题描述】:

下面的代码给了我一些下一个错误。 我要做的就是输入数据,然后按钮应该发出警报并输出输入的数据。

<template>
  <div class="hello">
    <h1> {{ greeting }} </h1>
    <label for="value"> Name </label>
    <input type="text" v-model="value" placeholder="input name">
    <button type="submit" v-on:click="clickHandler" class="btn btn-primary">Submit</button>
  </div>
</template>
<script src="./main.js"></script>


<script>
export default {
  name: 'input-data',
  data: function() {
    return {
      value: String,
      greeting: "Welcome to Heaven Stranger"
    }
  },
  methods: {
    clickHandler: function() {
      alert("Name of SIR: " + value);
    }
  }
}

【问题讨论】:

  • 如果你想访问组件数据不要忘记这个点“this.

标签: javascript vue.js


【解决方案1】:
methods: {
    clickHandler: function() {
      alert("Name of SIR: " + this.value);
    }
  }

将您的代码发送至sandbox and play

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
  • 添加了相关部分
猜你喜欢
  • 2020-10-04
  • 2018-05-27
  • 2017-06-05
  • 2019-09-16
  • 2019-06-23
  • 2021-05-16
  • 2019-11-13
  • 2016-11-07
  • 1970-01-01
相关资源
最近更新 更多