Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

vuejs2 - Conditional operator less than is not working in Vuejs

i have an issue with less than operator in vuejs. what i want is:

if original price is 10 and discount price 200, then the result (discount_price) should not be displayed, 
else show the discount_price

The code doesn't seem to work. Here is the code: HTML:

        <div v-if="original_price > discount_price">
            <span class="price-info mr-2">
              {{ original_price | numeral('0,0') }}
            </span>
            <span>
                {{ Number((original_price - discount_price) / original_price) * 100 + '%' }}
            </span>
        </div>

Please assist

question from:https://stackoverflow.com/questions/66057845/conditional-operator-less-than-is-not-working-in-vuejs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your v-if condition is not in the appropriate tag. It should be in the span of discount. Working demo below:

new Vue({
  el: '#app',
  data() {
    return {
      original_price: 10,
      discount_price: 20
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id='app'>
  <span class="price-info mr-2">
                  {{ original_price | numeral('0,0') }}
     </span>
  <span v-if="original_price > discount_price">
     {{ Number((original_price - discount_price) / original_price) * 100 + '%' }}
      </span>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...