Narutocc / Vue

:smirk_cat:Vue is a progressive framework for building user interfaces. Vue is also perfectly capable of powering sophisticated Single-Page Applications when used in combination with modern tooling and supporting libraries.
1 stars 0 forks source link

computed配合map方法 #94

Open Narutocc opened 3 years ago

Narutocc commented 3 years ago

判断数组中是否包含某个数据,可以使用computed,配合map方法

data () {
  return {
    originData: [{
      attr: '颜色',
      attrValues: ['黑', '白', '红']
    }, {
      attr: '尺寸',
      attrValues: ['大', '中', '小']
    }, {
      attr: '重量',
      attrValues: ['500g', '1kg', '5kg']
    }]
  }
},
computed: {
  selectAttr () {
    return this.originData.map(v => v.attr) // ['颜色', '尺寸', '重量']
  }
},
mounted () {
  console.log(this.selectAttr.includes('颜色')) // true
}
zehuichan commented 3 years ago
[Vue warn]: Error in render: "TypeError: _vm.selectAttr is not a function"
Narutocc commented 3 years ago

add includes()