Vue的JS计算的财产不会被触发有了这个标记vue.js不会触发计算财产
<!-- language: lang-html -->
<p>£{{plant_price}}</p>
<div v-if="selected.plant.variations.length > 0 ">
<select v-model="selected.plant.selected_variation" class="form-control">
<!-- inline object literal -->
<option v-for="(variation, i) in selected.plant.variations" :selected="variation.id == selected.plant.selected_variation ? 'selected' : ''":value="variation.id">
{{variation.name}}
</option>
</select>
</div>
<!-- language: lang-js -->
var app = new Vue({
el: '#vueApp',
data: {
selected: {
type: {a: '' , b: ''},
vehicle: '',
plant: {
}
},
computed: {
plant_price: function() {
if (this.selected.plant.variations.length > 0) {
var variant = _.find(this.selected.plant.variations, {id: this.selected.plant.selected_variation });
return variant.price;
} else {
return this.selected.plant.price;
}
}
...
selected.plant
通过点击植物填充 - 触发updateSelected方法。
<div class="col-sm-4" v-for="(plant, i) in step2.plants">
<div v-on:click="updateSelected(plant)" ....
methods: {
updateSelected: function(plant) {
this.selected.plant = plant; // selected plant
if (this.selected.plant.variations.length > 0) {
this.selected.plant.selected_variation = this.selected.plant.variations[0].id; // set the selected ID to the 1st variation
我已通过调试器检查过,可以看到所有正确的属性都可用。
selected:Object
type:Object
vehicle: "Truck"
plant:Object
id:26
price:"52"
regular_price:"100"
selected_variation:421
variations:Array[2]
0:Object
id:420
name:"small"
price:52000
regular_price:52000
1:Object
etc...
我有一个计算的属性,它应该基于对selected.plant.selected_variation
值更新plant_price
。
我抓住selected.plant.selected_variation
并搜索变化以检索价格。如果不存在变化,则给出工厂价格。
我有一个方法在每个产品更新选定的工厂。点击产品填充selected.plant并触发计算的plant_price更新价格(因为selected.plant.selected_variation的值已更改)。
但是,计算出的plant_price不是由select触发的。选择一个新的变种做它应该的,它更新selected.plant.selected_variation。但我的工厂价格似乎并没有被它触发。
所以我通过un-nesting selected.plant.selected_variation重构我的代码。我现在把它挂在数据对象上,因为
data = {
selected_variation: ''
}
并改变我的计算机属性来引用数据为this.selected_variation。我的计算属性现在可用?这对我来说没有意义?
感谢@SpeekaDievs,对不起 - 我没有跟我描述它解释自己很好......我会更新我的问题 – Rob
您的答案是正确的问题。我只是没有很好地描述它... – Rob