简体   繁体   English

有没有办法在 VUE html 中编写“如果大于”和“如果小于”语句?

[英]is there a way to write "If Greater than" and "If less than" statement in VUE html?

I've stumbled upon a problem recently, to which I've found a temporary solution.我最近偶然发现了一个问题,我找到了一个临时解决方案。

I'd like to know whether it's possible to write a statement like,我想知道是否可以写这样的声明,

If(a>b)

in HTML...在 HTML...

I'm currently working in vue.js, and I've found a temporary solution in v-if with & , ||我目前在 vue.js 工作,我在v-if中找到了一个临时解决方案,带有& , || , ! , ! operators, but greater than or less than doesn't work (which is a permanent solution to my problem).运算符,但大于或小于不起作用(这是我的问题的永久解决方案)。

Here's my temporary solution:这是我的临时解决方案:

v-if="models[0].paidDate.contains('2018') || models[0].paidDate.contains('2019') || models[0].paidDate.contains('2017')"

Any help will be greatly appreciated.任何帮助将不胜感激。

Since you have mentioned you're using Vue.js, I think what you're after is conditional rendering:既然您提到您正在使用 Vue.js,我认为您所追求的是条件渲染:

https://v2.vuejs.org/v2/guide/conditional.html https://v2.vuejs.org/v2/guide/conditional.html

You can do what you have asked like this:你可以像这样做你所要求的:

<div id="app">
  <p v-if="number > 5">{{number}} is greater than 5</p>
  <p v-else>{{number}} is less than 5</p>
</div>


<script>
    new Vue({
      el: '#app',
      data: {
        number: 2
      }
    });
</script>

Here is a fiddle: https://jsfiddle.net/nimeshka/0q5ynm4d/1/这是一个小提琴: https ://jsfiddle.net/nimeshka/0q5ynm4d/1/

Hope it helps :)希望能帮助到你 :)

Yes, it's possible to write <p v-if="a > b"> to conditionally render the <p> element when a is greater than b (and similarly for v-if="a < b" ).是的,可以编写<p v-if="a > b">以在a大于b有条件地渲染<p>元素(对于v-if="a < b" )。

 new Vue({ el: '#app', data() { return { a: 1, b: 2, c: 3, d: 4 }; } })
 <script src="https://unpkg.com/vue@2.5.16"></script> <div id="app"> <p v-if="a > b">a is greater than b</p> <p v-else>a is less than or equal to b</p> <p v-if="c < d">c is less than d</p> <p v-else>c is greater than or equal to d</p> </div>

Given your example, I'm assuming you're trying to compare two date strings by year (where models[0].paidDate is a date string).鉴于您的示例,我假设您正在尝试按年份比较两个日期字符串(其中models[0].paidDate是日期字符串)。 You'd have to convert that to a Date object before doing the comparison:在进行比较之前,您必须将其转换为Date对象:

<p v-if="new Date(models[0].paidDate).getFullYear() > 2017">

 new Vue({ el: '#app', data() { return { models: [ { paidDate: '2017-10-11T21:11:44.741Z' }, { paidDate: '2018-01-02T11:15:30.233Z' }, { paidDate: '2016-05-23T09:19:25.445Z' }, { paidDate: '2015-08-03T20:47:54.777Z' }, { paidDate: '2019-11-05T23:36:21.098Z' }, { paidDate: '2020-09-19T08:22:10.112Z' }, ] }; } })
 <script src="https://unpkg.com/vue@2.5.16"></script> <div id="app"> <h3>All Dates</h3> <ul> <li v-for="(model, index) in models"> {{index}} - {{new Date(model.paidDate).toString()}} </li> </ul> <h3>Dates after 2017</h3> <ul> <li v-for="(model, index) in models" v-if="new Date(model.paidDate).getFullYear() > 2017"> {{index}} - {{new Date(model.paidDate).toString()}} </li> </ul> </div>

As already answered above, you may go ahead and use conditional rendering as is within the template.正如上面已经回答的那样,您可以继续使用模板中的条件渲染。 However, your condition is not very straightforward and it takes some time to figure out what you're after.但是,您的情况不是很简单,需要一些时间才能弄清楚您在追求什么。 As Vue documentation suggests, consider using Computed property in this case.正如 Vue 文档建议的那样,在这种情况下考虑使用Computed 属性

<div id="app">
  <h2>Models:</h2>
  <ul>
    <li v-for="model in modelsInDateRange">
        {{model.name}}, paid on: {{model.paidDate}}
    </li>
  </ul>
</div>

<script>
new Vue({
    el: "#app",
    data: {
       startYear: 2016,
       endYear: 2017,
       models: [
          { paidDate: '2018-10-08', name: 'Model 2018'},
          { paidDate: '2017-10-08', name: 'Model 2017'},
          { paidDate: '2016-10-08', name: 'Model 2016'},
          { paidDate: '2015-10-08', name: 'Model 2015'},
      ]
    },
    computed: {
        modelsInDateRange() {
            return this.models.filter(this.modelInDateRange);
        }
    },
    methods: {
        modelInDateRange(model) {
           const year = new Date(model.paidDate).getFullYear();
           if (year >= this.startYear && year <= this.endYear) {
              return true;
           }
           return false;
        }
    },
});
</script>

I made the example a bit more thorough to show the overall idea.我让这个例子更彻底地展示了整体的想法。 In the template I loop through array of models that results from the filter method in the computed property.在模板中,我循环遍历由计算属性中的 filter 方法产生的模型数组。 That relies on a method that determines whether the model falls into the specified date or not.这依赖于确定模型是否属于指定日期的方法。 You can find the example here .您可以在此处找到示例。

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

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