How to use data in filters?

Use the filters function in vue2.x. If you want to use this in the filter to call the data in data, an error will be reported because this does not point tovueComponent instance.

If you want to use the data in data, there are generally two ways:

1. Pass in the data in data when using filter, such as:

<template>
  <div>
    目前选择的是:{ { type | convertText(list) }}
  </div>
</template>
 
<script>
export default {
  filters: {
    // 传进来list参数
    convertText: function(value, list) {
      let result = ''
      list.forEach(item => {
        if(item.value === value) {
          result = item.label
        }
      })
      return result
    }
  },
  data() {
    return {
      list: [
        {label: '苹果', value: 1},
 

おすすめ

転載: blog.csdn.net/BenChiZhuBaDaoWang/article/details/134051777