原因:filters中的this指向的并不是vue实例
通过以下方式:可以实现filters中获取到data的变量
1: 定义全局变量
_this(let _this = this)
2:beforeCreate() { _this = this; }//将this赋值给_this
3: filters下的方法可以直接使用_this.data来获取到data中的变量
<template>
<div>{{ status | filtTest }}</div>
</template>
<script type="text/ecmascript-6">
let _this = this //**定义全局变量**
export default {
data(){
statusDatas: [
{ value: 1, name: '对' },
{ value: 2, name: '否' }
]
},
beforeCreate() {
_this = this; //** 赋值 **
},
filters: {
filtTest(value) {
let newName = ''
_this.statusDatas.forEach(item => { //** 直接获取使用 **
if(item.value === value) {
return newName = item. name
}
})
return newName
}
}
}
</script>
