时间:2023-01-01 12:42:23 | 栏目:vue | 点击:次
用filters计算data中 a+b 的值
<template>
<div>{{a|sum(that)}}</div>
</template>
<script>
export default {
name: "test",
data() {
return {
that: this,
a: 1,
b: 2
}
},
filters: {
sum(a, that) {
console.log(that);
return a + that.b;
}
},
}
</script>
所以在filter中无法直接访问当前vue实例, 所以可以使用computed替代。
可是当遇到需要根据html文本改变,v-for的数据等情况而改变时,computed的功能就无法满足我们的需求了。
那我们就可以使用methods代替
data: {
shopItemType: {}
},
methods: {
shopItemType2str(id){
return this.shopItemType[id];
}
}
<tr v-for="shopItem in shopItems">
<td>{{shopItemType2str(shopItem.item_type)}}</td>
</tr>