computed替代filters 定义一个带自定义参数的计算函数

338 阅读1分钟

1.dom结构

<template slot-scope="scope">
  {{roleNameFn(id)}}
</template>

2.js结构

data () {
    return {
      id:'asdfasfdfasf',
      allRoles: [
        {
          id: 'asdfasfdfasf',
          position: '返回值'
        }
      ],
    }
  },
computed: {
	roleNameFn () {
    	return function (id) {
        let val = ''
        let obj = this.allRoles.find(item => {
          return id === item.id
        })
        if (obj) {
          val = obj.position
        }
        return val
      }
    }
}