简体   繁体   English

如何使用 v-model 和 v-if 使用 Vue.js 构建一个简单的计算器?

[英]How can I build a simple calculator with Vue.js, using v-model and maybe v-if?

I need to build a simple component in Vue.Js where there is an input field that is showing number of hours an user would save if they switch to our software.我需要在 Vue.Js 中构建一个简单的组件,其中有一个输入字段显示用户在切换到我们的软件时可以节省的小时数。 How can I use the v-if directive:如何使用 v-if 指令:

If they are spending 20 - 30 hours, they would save 10 hours / mo, 30 - 50 hours, they would save 20 hours / mo.如果他们花费 20 - 30 小时,他们将节省 10 小时/月,30 - 50 小时,他们将节省 20 小时/月。 Here is my setup for html:这是我的 html 设置:

<div id="app">
<label> How Many Hours Do You spent per Month doing Invoicing
<input v-model="{{ hours }}" />
<p v-if="hours <=20 "> You could save {{hours}} hours every month </p>
<p v-else-if="hours > 20 ">You could save {{hours}} hours every month</p>
</div>
new Vue({
    el: '#calc',
         data() {
            return {
               hours:
               save: 
                   }
       },

Hi anaisUx and welcome to SO.嗨anaisUx,欢迎来到SO。

You need to produce some kind of code example.您需要生成某种代码示例。 What have you tried?你试过什么? What doesn't work?什么不起作用?

But to answer your question about v-if: v-if isn't gonna help you with your problem.但是要回答您关于 v-if 的问题: v-if 不会帮助您解决问题。 v-if is used in VueJS templates to indicate if the element should be rendered or not. v-if 在 VueJS 模板中用于指示是否应该渲染元素。 What you are describing here is some kind of business logic, that you shouldn't put in your templates but inside your compenent and then render the results in your template.您在这里描述的是某种业务逻辑,您不应该将其放入模板中,而是放入您的组件中,然后在模板中呈现结果。

Without beeing too harsh it looks like you are very new to VueJS and programming in general, so I'm gonna guide you in the right direction.不用太苛刻,看起来您对 VueJS 和一般编程都很陌生,所以我将引导您朝着正确的方向前进。

  1. Create your VuuJS app: https://v2.vuejs.org/v2/guide/#Getting-Started创建您的 VuuJS 应用程序: https ://v2.vuejs.org/v2/guide/#Getting-Started

  2. Create your input (time spent) and bind it to a data-property: https://v2.vuejs.org/v2/guide/#Handling-User-Input创建您的输入(花费的时间)并将其绑定到数据属性: https ://v2.vuejs.org/v2/guide/#Handling-User-Input

  3. Time saved is dependant on time spent, so computed property looks like something that might work: https://v2.vuejs.org/v2/guide/computed.html节省的时间取决于花费的时间,因此计算属性看起来可能有效: https ://v2.vuejs.org/v2/guide/computed.html

  4. Write your business logic with simple if statements.使用简单的 if 语句编写您的业务逻辑。

In other words:换句话说:

  1. Create a data-property named timespent创建一个名为 timespent 的数据属性
  2. Create a input in your template and bind it to timespent在您的模板中创建一个输入并将其绑定到所花费的时间
  3. Create a computed property called timesaved that uses timespent to calculate how much time the customer would save.创建一个名为timesaved 的计算属性,它使用timespent 来计算客户将节省多少时间。
  4. Render timesaved in your template在您的模板中节省渲染时间

Expected result: As soon as a customer types something in your input the VueJS app should render timesaved to the template.预期结果:一旦客户在您的输入中键入内容,VueJS 应用程序应该将节省的时间渲染到模板。 This should update automatically.这应该会自动更新。

You can practice on codepen, jsfiddle, codesandbox.io/s/vue or any other similar site and post your code here to get some feedback.您可以在 codepen、jsfiddle、codesandbox.io/s/vue 或任何其他类似网站上练习,并在此处发布您的代码以获得一些反馈。

Edit: Since you've made an effort and tried a little, here is a working example:编辑:由于您已经努力并尝试了一点,这里是一个工作示例:

https://codepen.io/bergur/pen/WqPjNo https://codepen.io/bergur/pen/WqPjNo

new Vue({
  el: '#app',
  data() {  
    return {
      hours: ''       
    }
  },
  computed: {
    save() {
      if (this.hours > 0 && this.hours < 20) {
        return 1
      }
      if (this.hours >= 20 && this.hours < 30) {
        return 10
      }
      
      if (this.hours >= 30 && this.hours < 50) {
        return 20
      }            
      
      if (this.hours >= 50) {
        return 30
      }            
    }
  }
})

Data property hours stores how many hours the customer is spending on invoicing.数据属性小时数存储客户在开发票上花费的小时数。 Like i mentioned above there is no need for multiple v-if, the business logic should stay in the component.就像我上面提到的,不需要多个 v-if,业务逻辑应该留在组件中。

I used a computed property save wjich stores the hours the custumer can save.我使用计算属性 save wjich 存储客户可以节省的时间。

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

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