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