使用vue.js递增和呈现文本

使用vue.js递增和呈现文本,vue.js,vuejs2,Vue.js,Vuejs2,我正在尝试做一些非常简单的事情。递增一个数字: <div> <button v-on:click="increase">Add 1</button> <p>{{ sayHello() }} and {{ counter }}</p> </div> 将数据放在我得到的方法之外时: 您将数据放在方法中,它应该在根级别,因为我们在一个组件中: 你是对的,我完全忘了我们在一个组件中,我马上编辑这个 <b-form

我正在尝试做一些非常简单的事情。递增一个数字:

<div>
   <button v-on:click="increase">Add 1</button>
   <p>{{ sayHello() }} and {{ counter }}</p>
</div>
数据
放在我得到的方法之外时:


您将
数据
放在
方法
中,它应该在根级别,因为我们在一个组件中:


你是对的,我完全忘了我们在一个组件中,我马上编辑这个
<b-form-input v-bind="renderText(counter)"
    type="text"
    placeholder="Type Alex"></b-form-input>
<b-form-input v-bind="foo"
   type="text"
   placeholder="Enter your name"></b-form-input>

 <p>Value: {{ foo }} and {{counter}}</p>
<template>
    <div>
        <form>
            <div>
                <button v-on:click="increase">Add 1</button>
                <p>{{ sayHello() }} and {{ counter }}</p>
            </div>
            <div>
                <b-form-input v-bind="foo"
                              type="text"
                              placeholder="Enter your name"></b-form-input>

        <b-form-input v-bind="renderText(counter)"
                   type="text" placeholder="Type Alex"></b-form-input> 
               <p>Value: {{ foo }} and {{counter}}</p>
              </div>
          </form>
      </div>
</template>

<script lang="ts">

    import Vue from 'vue';


    export default Vue.extend({

        name: 'Hobbies',

        methods: {

            data:{
              foo: '',
              counter: 0
            },

            sayHello: function () {
                return 'Alex'
            },

            increase: function () {
                this.counter++;
            },
            renderText: function (event) {
                if (event == 5){
                    return 10
                } else{
                    return 16
                }
            }
        }

    });

</script>

<style scoped>

</style>
export default {
  name: 'Hobbies',
  data: function(){
    return { foo: '', counter: 0 };
  },
  methods: {
    sayHello: function () { return 'Alex' },
    increase: function () { this.counter++; },
    renderText: function (event) {
      if (event == 5){
        return 10
      } else{
        return 16
      }
    }
  }
}