필드가 여러 개인 테이블 행이있는 구성 요소가 있습니다. 한 필드를 업데이트하면 여백이나 판매 가격을 기준으로 다른 필드가 업데이트됩니다.VueJs Watchers를 동적으로 관리하기
그러나 모든 분야를 지켜보고있는 중 나는 튀는 효과를 얻고 있습니다. _debounce를 추가하면 문제가 해결되지만 도움이되지는 않습니다. 시도하고 문제를 관리하기 위해 unwatch() 트리거하려면 감시자에게 콜백을 사용하고 있지만 내가 다시 관찰자를 추가 할 때 콜백 멈추지 않는 중지합니다.
나는 코드의 예로 요점을 지었다.
Vue.component('pricing', {
template: '#pricing-row',
props: ['item'],
mounted() {
this.addWatchers()
},
methods: {
resetWatchers() {
setTimeout(()=> {
this.addWatchers()
}, 700)
},
addWatchers() {
this.updateNet = this.$watch(
function() {
return this.item.net
},
function() {
// unmount other watchers
this.updateMargin()
this.updateSell()
// calculate sell price and update
this.setSellPrice()
// re-add watchers
this.resetWatchers()
}
),
this.updateMargin = this.$watch(
function() {
return this.item.margin
},
function() {
// unmount other watchers which can cause bounce effect
this.updateSell()
// calculate sell price and update
this.setSellPrice()
// re-add watchers
this.resetWatchers()
}
),
this.updateSell = this.$watch(
function() {
return this.item.sell
},
function(sellPrice) {
// unmount other watchers which can cause bounce effect
this.updateMargin()
// update margin
this.setMargin(sellPrice)
// re-add watchers
this.resetWatchers()
}
)
},
setSellPrice() {
let price = (100/(100 - this.item.margin)) * this.item.net
this.item.sell = price.toFixed(2)
},
setMargin(sellPrice) {
let profit = (sellPrice - this.item.net)
let price = (100 * profit)/sellPrice
this.item.margin = price.toFixed(2)
}
}
})
new Vue({
el: '#vue',
data: {
prices: [
{
id: 1,
net: 5,
margin: 10,
sell: 5.56
},
{
id: 2,
net: 7,
margin: 10,
sell: 7.78
},
]
}
})
은 내가 메소드를 호출
mounted()
에 그들을 장착하여 올바르게 전문가를 사용하고 생각합니다. 그리고 그 방법을 리콜하여 다시 초기화합니까?
정말 도움이되기를 바랍니다.
이 매우 불행한 솔루션을 것을 볼 수 있습니다. 'net','margin'과'sell' 속성 사이의 관계 만 설명해 주시겠습니까? – WaldemarIce
기본적으로 판매 가격을 업데이트하면 그물 및 마진을 기준으로 판매 가격이 자동으로 다시 계산됩니다. 다른 필드와 동일합니다. – Lee
이것은 무한 루프입니다. 지금 나는 문제가 어디 있는지 이해하고있다. 가장 단순한 솔루션은'sell' 속성을 원가로 고려하고있다. 직접 사용할 수 없다. 실제 판매 가격은 그물, 마진 및 원가를 기반으로 계산 된 속성을 사용해야한다. – WaldemarIce