need:In vue, in addition to using filters in templates, sometimes, filters in filters are also needed in methods.
Solutions proposed by netizen hongz1125:
this.$[filter](...args) //This method is very simple,Very practical, too
Here is my method, which is a bit complicated. It is recommended to use the method mentioned above by netizens.
filters: { formatScore(score) { if (score < 20) { score = 'Failed'; } else if (score >= 20 && score <= 27) { score = 'qualified'; } else if (score >= 28 && score <= 31) { score = 'good'; } else if (score > 31) { score = 'excellent'; } return score } }, methods: { formatScore(score) { if (score < 20) { score = 'Failed'; } else if (score >= 20 && score <= 27) { score = 'qualified'; } else if (score >= 28 && score <= 31) { score = 'good'; } else if (score > 31) { score = 'excellent'; } return score }, getPhysicalResult() { this.$('/rc_ChildTest/testResult').then((res) => { = (scoreparameter);//Use the filter method, you need to write a exactly the same method separately. Called via this }) }, },
Solution:
1. Create a public file and extract and encapsulate public js
export default { formatScore(score) { if (score < 20) { score = 'Failed'; } else if (score >= 20 && score <= 27) { score = 'qualified'; } else if (score >= 28 && score <= 31) { score = 'good'; } else if (score > 31) { score = 'excellent'; } return score }, }
2. Import and use
import report from 'JS file location'
filters: { formatScore(score) { return (score);//Use the imported method } }, methods: { getPhysicalResult() { this.$('/rc_ChildTest/testResult').then((res) => { = (scoreparameter);//The imported method is directly used here. The methods in the filter can be used in the methods method. }) }, },
This is how I solved it. If you have a better way, I hope you will write it in the comments and you are welcome to criticize and correct me.
The above example of calling filters in filters in Vue methods is all the content I share with you. I hope you can give you a reference and I hope you can support me more.