vue.js - 为什么 filter() 中的 `this` 在 VueJS 中未定义?

标签 vue.js vuejs2

我正在创建 DOB 表单。

我在表单中使用 VueJS。用户应将月份的日期放在首位,以便根据相应月份的天数显示日期。

我正在使用 filter(),问题是 filter() 中的 this 未定义。我该如何解决这个问题?

new Vue ({
  el: '.app',
  data: {
    months: [
      {month: 'January', days: 31},
      {month: 'February', days: 28},
      {month: 'March', days: 31},
      {month: 'April', days: 30},
      {month: 'May', days: 31},
      {month: 'June', days: 30},
      {month: 'July', days: 31},
      {month: 'August', days: 31},
      {month: 'September', days: 30},
      {month: 'October', days: 31},
      {month: 'November', days: 30},
      {month: 'December', days: 31},
    ],
    selectedMonth: []
  },
  computed: {
    filterDays() {
      return this.months.filter(function(value) {
        return value.month === this.selectedMonth;
      });
    }
  },
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>

<div class="app">
  <select id="dobd">
    <option v-for="day in filterDays[0].days" :value="day">{{ day }}</option>
	</select>
</div>

我知道使用全局变量可能是解决方案,但由于我自己的需要,我想在 data() 中使用 selectedMonth

最佳答案

使用 function () {} 上下文 (this) 丢失。这意味着,在 filter 参数函数中,this 不会是 Vue 实例。

有一些可能的解决方案:

  • 使用箭头函数(首选):

    filterDays() {
      return this.months.filter((value) => {
        return value.month === this.selectedMonth;
      });
    }
    
  • 使用.bind():

    filterDays() {
      return this.months.filter(function(value) {
        return value.month === this.selectedMonth;
      }.bind(this));
    }
    
  • 在函数外使用局部变量:

    filterDays() {
      let vm = this;
      return this.months.filter(function(value) {
        return value.month === vm.selectedMonth;
      });
    }
    

演示:

new Vue ({
  el: '.app',
  data: {
    months: [
      {month: 'January', days: 31},
      {month: 'February', days: 28},
      {month: 'March', days: 31},
      {month: 'April', days: 30},
      {month: 'May', days: 31},
      {month: 'June', days: 30},
      {month: 'July', days: 31},
      {month: 'August', days: 31},
      {month: 'September', days: 30},
      {month: 'October', days: 31},
      {month: 'November', days: 30},
      {month: 'December', days: 31},
    ],
    selectedMonth: 'January' // changed to a valid month
  },
  computed: {
    filterDays() {
      return this.months.filter((value) => {
        return value.month === this.selectedMonth;
      });
    }
  },
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>

<div class="app">
  <select id="dobd">
    <option v-for="day in filterDays[0].days" :value="day">{{ day }}</option>
  </select>
</div>

关于vue.js - 为什么 filter() 中的 `this` 在 VueJS 中未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49714015/

相关文章:

vue.js - 运行 Vue 应用程序时出现错误 "vue-cli-service: command not found"

javascript - 使用 Vue 添加/删除动态 DOM 元素

javascript - Vue.js 状态更新但 View 不重新渲染

javascript - Vue 组件不会在新的 $router.push 上重新渲染

laravel - 如何在我的 View 端组件中显示 vue-good-table 中的自定义内容?

vue.js - 如何重置 vue-infinite-loading 元素?

javascript - VueJS - 如何从 prop 评估花括号语法

vue.js - Vue-Router 和 Gsap ScrollTrigger

javascript - Vue 路由器 beforeEnter 与 beforeEach

javascript - 选择不工作的 VueJS 默认值