current position:Home>Summary of filter scheme used in JS code in Vue

Summary of filter scheme used in JS code in Vue

2022-05-15 02:07:01Wooden house x

vue Inside JS The code uses filter Summary of the plan

1. Demand background
  • Recently vue Used in the project filter, A common situation is in vue Used in template , There are also some situations in JS The code uses , Here's a summary :
Solution
  • // 1. Add one globally filter(Vue Introduced separately ), Such as :
    Vue.filter('dictionary', function(value, code) {
          
        if (code) {
          
           return localStorage.get('DICT_DATA')[code];
        }
        return ''
    })
    
    // 2. Use... In templates :
    <span>{
          {
          value|dictionary('documents_type')}}</span>
    
    // 3. stay JS Use in , Method 1:
    this.$options.filters['dictionary'](value,'documents_type')
    
    // 4. stay JS Use in , Method 2:
    Vue.filter('dictionary')(value,'documents_type')
    
    

copyright notice
author[Wooden house x],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/132/202205120527037631.html

Random recommended