【问题标题】:Vuejs - Undefined when calling method functionVuejs - 调用方法函数时未定义
【发布时间】:2021-09-30 18:53:40
【问题描述】:

在VUE组件JS下面使用,在ajax成功内调用“deleteuserParticulars”函数。但是,没有定义获取“deleteuserParticulars”。

不确定我错过了哪个并拨打了这个电话。可以帮助尽快解决这个问题吗?谢谢

import Vue from 'vue';
const userComponent = Vue.component('user-form-component', {
  template: componentHTML(),
  props: ['saveddata'],

  components: {
    userParticularsModalComponent
  },
  data: function () {
    return {
      userDetails: []
    }
  },
  methods: {
    deleteuser: function (newUser) {
      let deleteDraftEndpointUrl = $('.main-component').attr('data-delete');
      $.ajax({
        url: deleteDraftEndpointUrl + newUser['draftId'],
        type: 'GET',
        success: function(s) {
          if(s.status == 'success'){
            this.deleteuserParticulars();
          }
        },
        error: function(){
          console.log('Error on delete user', error);
        }
      });
      
    },
    deleteuserParticulars: function(){
      this.userDetails = this.userDetails.filter((user) => (user['info'].PP !== newuser['info'].PP);
      this.userAllDetails = this.userDetails;
      this.$emit('user', this.userDetails);
    }
  },
  mounted: function () {
  },
  updated: function () {
    console.log('U', this.waitForUpdate);
  }
});

export default userComponent;

【问题讨论】:

标签: vue.js vuejs2


【解决方案1】:

您需要使用粗箭头函数来摆脱this 范围。试试这个sn-p

import Vue from 'vue';
const userComponent = Vue.component('user-form-component', {
  template: componentHTML(),
  props: ['saveddata'],

  components: {
    userParticularsModalComponent
  },
  data: function () {
    return {
      userDetails: []
    }
  },
  methods: {
    deleteuser: function (newUser) {
      let deleteDraftEndpointUrl = $('.main-component').attr('data-delete');
      $.ajax({
        url: deleteDraftEndpointUrl + newUser['draftId'],
        type: 'GET',
        success: (s) => { // the fix is here
          if(s.status == 'success'){
            this.deleteuserParticulars();
          }
        },
        error: function(){
          console.log('Error on delete user', error);
        }
      });
      
    },
    deleteuserParticulars: function(){
      this.userDetails = this.userDetails.filter((user) => (user['info'].PP !== newuser['info'].PP);
      this.userAllDetails = this.userDetails;
      this.$emit('user', this.userDetails);
    }
  },
  mounted: function () {
  },
  updated: function () {
    console.log('U', this.waitForUpdate);
  }
});

export default userComponent;

欲了解更多信息:https://stackoverflow.com/a/34361380/10362396

【讨论】:

  • 仍然显示“deleteuserParticulars”未定义
猜你喜欢
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 2011-06-16
  • 1970-01-01
  • 2014-05-01
  • 1970-01-01
  • 2021-02-16
  • 2013-10-30
相关资源
最近更新 更多