【问题标题】:Vue.js this is undefined inside computed propertyVue.js this 在计算属性中未定义
【发布时间】:2019-03-18 18:23:22
【问题描述】:

我有以下带有模型selectedProp的输入标签:

<input type="text" v-model="selectedProp" />

我想遍历这样的项目:

<div v-for="item of filteredItems">{{item.prop}}</div> 

这是组件的脚本:

export default {
  name: 'App',
  data() {
    return {
      items: [],
      selectedProp: "",
      projects: [],
      errors: []
    }
  },
  created() {
   axios.get(`${URL}`)
   .then(response => {
      // JSON responses are automatically parsed.
      this.items = response.data;
    })
    .catch(e => {
      this.errors.push(e)
    });

  },

  computed: {
    filteredItems() {
      if(this.selectedProp) {
        console.log(this.selectedProp);
        return this.items.filter(function (item) {
          return item.prop == this.selectedProp;
        });

      }
      return this.items;

    }
  },
}

错误

这是在计算属性中未定义的

【问题讨论】:

    标签: javascript vue.js vuejs2 axios vue-component


    【解决方案1】:

    在这种情况下,您可以使用可以访问this 对象的箭头函数

     return this.items.filter( (item)=> {
          return item.prop == this.selectedProp;
        })
    

    【讨论】:

      猜你喜欢
      • 2018-01-25
      • 2017-07-29
      • 2018-04-09
      • 2015-09-25
      • 2019-08-04
      • 2020-08-04
      • 2017-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多