简体   繁体   English

如何将计算参数从一个 Vue 组件传递到另一个?

[英]How to pass a computed parameter from one Vue component to another?

I have one app.js file which inlcudes three Vue components and the vue instance.我有一个app.js文件,其中包含三个 Vue 组件和 vue 实例。 In the first two components the user puts in values.在前两个组件中,用户输入值。 The third component then is supposed to make a simple calculation and display the result.然后,第三个组件应该进行简单的计算并显示结果。

In the code excerpt below you see the second component ("cost-input") and the third component ("result-output").在下面的代码摘录中,您会看到第二个组件(“成本输入”)和第三个组件(“结果输出”)。 As you can see, I try to use the computed parameter developmentCost , from the second component, and use it as data inside the result-output component - but that's wrong, not working and I'm clueless how to do that?如您所见,我尝试使用第二个组件中的computed参数developmentCost ,并将其用作result-output组件中的data - 但这是错误的,不起作用,我不知道该怎么做? I keep asking myself: How can it be so hard/complicated to "access" and read these values across different components which sit within the very same javascript file???我一直在问自己:“访问”和读取位于同一个 javascript 文件中的不同组件中的这些值怎么会如此困难/复杂?

// Revenue Input Component #1
...
// Cost Input Component #2
Vue.component('cost-input', {
  data: function () {
    return {
      durationDays: "10",
      costRate: "160",
      numberOfExperts: "1"
    }
  },
  computed: {
    developmentCost: function(e) {
      let dc = this.durationDays * this.costRate * 8 * this.numberOfExperts; 
      //return currencyFormat(c);
      //console.log(c);
      return dc;
    }
  },
  template: ...
})
// Result Output Component #3
Vue.component('result-output', {
  data: function () {
    return {
      revenuesFirst: "",
      developmentCost: ""
   }
  },
  computed: {
    calculatedProfits: function(e) {
      let cp = this.revenuesFirst - this.developmentCost; 
      //return currencyFormat(c);
      //console.log(cp);
      return cp;
    }
  },
  template: ...
})
//Vue instance
let app = new Vue({
  el: "#app",
});

Can someone help me understand how I re-use the developmentCost result (from the Cost Input Component #2 ) and calculate with it in the third component?有人可以帮助我了解如何重用developmentCost结果(来自Cost Input Component #2 )并在第三个组件中进行计算吗?

I read about "props", "import", "export", "vuex" but can't connect the dots.我读到了“props”、“import”、“export”、“vuex”,但无法连接这些点。 All those examples and fiddles out there are based on separate component files - whereas in my case everything happens inside one single app.js file.所有这些示例和小提琴都基于单独的组件文件——而在我的例子中,一切都发生在一个单独的app.js文件中。

Here is a link to my Codepen: https://codepen.io/bauhausweb/project/editor/ZqnkEO When you select the "Payments per Year" on the left side, you will see that the "Revenues" calculate nicely, and so does the "Cost" on the right side.这是我的 Codepen 的链接: https ://codepen.io/bauhausweb/project/editor/ZqnkEO 当您选择左侧的“每年付款”时,您会看到“收入”计算得很好,所以做右侧的“成本”。 All I'm trying to achieve is that the "Profits" component at the bottom displays the results from "component #1 minus component #2", Revenues minus Cost.我想要实现的是底部的“利润”组件显示“组件#1 减去组件#2”的结果,即收入减去成本。

I appreciate your help and taking the time to assist a curious but true Vue newbie感谢您的帮助并花时间帮助好奇但真正的 Vue 新手

What you could do is store the developmentCost value in the Vue root component and pass it down as a prop to the result-output component.您可以做的是将developmentCost值存储在 Vue 根组件中,并将其作为prop传递给result-output组件。

You would then use a watcher and v-model to synchronise the value.然后,您将使用观察者和v-model来同步值。

Vue.component('cost-input', {
  data: function () {
    return {
      durationDays: "10",
      costRate: "160",
      numberOfExperts: "1"
    }
  },
  computed: {
    developmentCost: function(e) {
      let dc = this.durationDays * this.costRate * 8 * this.numberOfExperts; 
      //return currencyFormat(c);
      //console.log(c);
      return dc;
    }
  },
  watch: {
    developmentCost: {
      handler: function (value) {
        this.$emit("input", value);
      },
      immediate: true
    }
  },
  template: ...
})
Vue.component('result-output', {
  props: ["developmentCost"],
  data: function () {
    return {
      revenuesFirst: ""
   }
  },
  computed: {
    calculatedProfits: function(e) {
      let cp = this.revenuesFirst - this.developmentCost; 
      //return currencyFormat(c);
      //console.log(cp);
      return cp;
    }
  },
  template: ...
})
let app = new Vue({
  el: "#app",
  data: {
    developmentCost: ""
  }
});
<cost-input v-model="developmentCost"></cost-input>
...
<result-output :development-cost="developmentCost"></result-output>

v-model documentation v-model文档

watchers观察者

Here is the codepen with the updated code这是带有更新代码的codepen

暂无
暂无

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

相关问题 如果 Vue 3 中的条件为真,如何将样式从一个组件传递到另一个组件 - How to pass a style from one component to another if condition is true in Vue 3 Vue JS 3:如何将数据从一个组件传递到另一个组件? - Vue JS 3: How to pass data from one component to another other? 如何将参数从 Vue root 传递到组件? - How to pass parameter from Vue root to component? 如何将数据(书籍数组长度)从一个组件传递到 vue.js 中的另一个组件? - How to pass data(books array length) from one component to another component in vue.js? Vue.js 如何将参数从一个组件传递到另一个组件? - Vue.js How to pass params from one component to another component? 如何将数据从一个组件传递到另一个组件并将该数据存储到 vue.js 中的数组中? - How to pass data from one component to another component and store that data into an array in vue.js? 如何将 href 参数从父 vue 组件传递给子组件? - How to pass href parameter from parent vue component to child component? 如何在 router.push() (Vue.js) 中将参数从一个组件传递到另一个组件? - How to pass parameters from one component to another in router.push() (Vue.js)? 如何将方法从一个组件传递到另一个组件并在 Vue.js 中使用它? - How to pass a method from one component to another and use it onclick in Vue.js? 如何将值从一个组件传递到另一个组件? - How can I pass values from one component to another component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM