目录Vue过滤器filters获取不到this对象原理下面举个例子Vue filters this指向问题Vue实例中filter不依赖于当前vue实例上下文vue过滤器filter
用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>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: 解决vue过滤器filters获取不到this对象的问题
本文链接: https://www.lsjlt.com/news/163409.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-12
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0