gpt4 book ai didi

javascript - 将 vuejs 函数绑定(bind)为上下文 vuejs 的数据

转载 作者:行者123 更新时间:2023-11-28 06:01:54 27 4
gpt4 key购买 nike

我想使用函数作为数据属性。这似乎在“works”数据属性的情况下工作得很好。但是我需要函数中的 this 上下文,以便我可以计算存储在 this.shoppingCart (另一个属性)中的值。

这可能吗?如果是这样我做错了什么?

 new Vue({
el: '#vueApp',

data: {

shoppingCart: [],

works : function () {
return "testfunc";
},
totalPriceCalcProperty : function () {

this.totalPrice = this.shoppingCart.reduce(function(total, cartItem){

console.log(total, cartItem);
return total + parseFloat(cartItem.price);

}, 0);
}
},

methods: {

totalPriceCalc: function () {

this.totalPrice = this.shoppingCart.reduce(function(total, cartItem){

console.log(total, cartItem);
return total + parseFloat(cartItem.price);

}, 0);
},
}

最佳答案

您应该使用方法而不是数据来实现此目的。
data 正在帮助您存储某些内容而不是处理某些操作。

在方法中,可以调用this.xxx从dataproperty获取属性

关于javascript - 将 vuejs 函数绑定(bind)为上下文 vuejs 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37209903/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com