vue中使用filters的注意事項
阿新 • • 發佈:2021-01-08
1.報錯:Failed to resolve filter: choseWords
在區域性使用filter的時候,一定要注意語法,稍微一疏忽就會報這個錯誤
filters:{ changeMoney(money){ return'¥'+money; } } 注意:這裡一定是filters,而不是filter,有時候手誤就寫成filter,結果導致報Failed to resolve filter的錯誤 全域性filter定義如下: Vue.filter('choseWords',function(money){ return'¥'+money; }) 2.filter中this指向的問題created
、mounted
、methods
中,this
指向的都是當前 的Vue
例項
在filter的this指向的卻是window
filters:{
choseWords(index){
console.log(this);
}
}
解決方法1:
如果filter裡面只需要一個引數,就把這個引數當做filter的第二個引數傳進去使用,在模板中:
<liv-for="(item,index)inaskList":key="index">
<divv-if="item.status==false">{{index|choseWords(word)}}{{item.askName}}</div>