没有显示文字

时间:2017-10-01 20:57:52

标签: javascript vue.js vue-component

我正在创建一个带有三个按钮的咖啡师小应用程序,而不是试图让文本显示"一个将是 "直到有人点击这三个组件中的一个。我知道我已经关闭,因此无法找到任何引用 这是我的HTML:

    <div id="app">

    <barista-template></barista-template>
</div>


    <!--template for Barista-->
<script type="text/x-template" id="b-template">
    <div>
        <div> one {{order_type}} that would be {{order_value}}</div>
        <button v-on:click="choose('Drip')">Drip</button>
        <button v-on:click="choose('Frenchpress')">French Press</button>
        <button v-on:click="choose('Aeropress')">Aeropress</button>
    </div>
</script>
</body>
</html>

这是我的Javascript

Vue.component("barista-template",{
    template: "#b-template",
    data: function () {
        return{
            user_order:"",
            computer:"",
            outcome:"",
            order_type:"",
            order_value: "",    
        }
    },
    methods: {
        choose: function (order_type) {
            this.order_type = order_type;

            if (this.user_order == "drip") {
                if (this.order_value == "drip") this.order_value = $10;

            }
            if (this.user_order == "frenchpress") {
                // if (this.computerMove == "frenchpress") this.outcome ="French press";

            }
            if (this.user_order == "Aeropress") {
                if (this.computerMove == "Aeropress") this.outcome ="Aeropress";

            }

        }
    }

});

new Vue ({
    el:"#app",
    data:function () {
        return{
            showing:false
        }
    }
}); 

1 个答案:

答案 0 :(得分:1)

我会使用一个计算器,但你可以用

进行内联

<div v-if="order_type !== ''"> one {{order_type}} that would be {{order_value}}</div>

计算只是使代码更具可读性

Vue.component("barista-template",{
  template: "#b-template",
  data: function () {
    return{
          order_type:"",
          order_value: "",    
      }
  },
  computed: {
    showText () {
      if(this.order_type === '') return '';
      return 'one ' + this.order_type + ' that would be ' + this.order_value
    }
  },
  methods: {
      choose: function (order_type) {
          this.order_type = order_type;

          if (this.order_type == "drip") {
              this.order_value = "$10";

          }
          if (this.order_type == "frenchpress") {
              this.order_value = "$20";
          }
          if (this.order_type == "aeropress") {
              this.order_value = "$30";
          }
      }
  }

});

new Vue ({
  el:"#app",
  data:function () {
      return{
          showing:false
      }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.4/vue.js"></script>
<div id="app">
  <barista-template></barista-template>
</div>

<!--template for Barista-->
<script type="text/x-template" id="b-template">
    <div>
    <div>{{showText}}</div>
    <button v-on:click="choose('drip')">Drip</button>
    <button v-on:click="choose('frenchpress')">French Press</button>
    <button v-on:click="choose('aeropress')">Aeropress</button>
  </div>
</script>