简体   繁体   English

VueJS2 - 计算属性/方法是设置Vue.data?

[英]VueJS2 - computed property/method is setting Vue.data?

I've been building a form with Vue, where I copy all the data I need to submit over to a separate params object, process it, and then submit that object through AJAX. 我一直在用Vue构建一个表单,在那里我将我需要提交的所有数据复制到一个单独的params对象,处理它,然后通过AJAX提交该对象。 Everything working fine with that, except for an annoying array which seems to directly set my Vue.data object. 除了一个似乎直接设置我的Vue.data对象的恼人数组之外,一切正常。

Here's my code: 这是我的代码:

getParams: function () {
  return {
    organism:           this.organism,
    species_id:         this.speciesID,
    bioinformatics:     this.bioinformatics,
    // ... more params
  }
}

sanitizeParams: function() {
  var params = this.getParams();

  // other validations here (that work fine)     

  if(this.isOther(this.bioinformatics)) {

    console.log(this.bioinformatics) // ['other']
    console.log(params.bioinformatics) // ['other']

    var idx = params.bioinformatics.indexOf('other');
    params.bioinformatics.splice(idx,1);

    console.log(this.bioinformatics) // [] <-- Why does this change?!
    console.log(params.bioinformatics) // []        

    params.bioinformatics.push(this.bioinformaticsOther);
  }
  return params;
},

saveForm: function () {
  var params = this.sanitizeParams();
  if(this.formValidation) {
    // send ajax with correct params
  }
},

Bioinformatics is a checkbox selection which includes "Other" in its options -- which, in turn, opens an input box where the user can insert his own text. 生物信息学是一个复选框选择,其选项中包含“其他” - 这反过来打开一个输入框,用户可以在其中插入自己的文本。 What I'm trying to accomplish is to remove 'other' from the params.data.bioinformatics array and add Vue.data.bioinformaticsOther to it. 我想要完成的是从params.data.bioinformatics数组中删除'other'并将Vue.data.bioinformaticsOther添加到其中。

What's happening is that my Vue.data.bioinformatics gets changed as well! 发生的事情是我的Vue.data.bioinformatics发生了变化! As soon as params.bioinformatics.splice runs, both arrays change, instead of just my local params object (as I expected it to happen). 一旦params.bioinformatics.splice运行,两个数组都会更改,而不仅仅是我的本地params对象(正如我预期的那样)。 This in turns causes my UI to stop working correctly since the option value stored by Vue in data is no longer 'other'. 这反过来导致我的UI停止正常工作,因为Vue在数据中存储的选项值不再是“其他”。 I started having these functions as computed properties but I changed them to methods to see if would solve the problems, but the same thing happens (hence the title). 我开始将这些函数作为计算属性,但我将它们更改为方法以查看是否可以解决问题,但同样的事情发生(因此标题)。

I'm still very new to Vue and JS in general so I'm sorry if it's really something obvious but I've been really dumbfounded for a while (especially since my other non-array options using the same method work just fine). 我对Vue和JS一般都很新,所以我很抱歉,如果它真的很明显,但我已经傻了一会儿(特别是因为我使用相同方法的其他非数组选项工作得很好)。

I'm sorry if this description is confusing, I'll add more data if needed. 如果这个描述令人困惑,我很抱歉,如果需要,我会添加更多数据。

EDIT: Added some console.log's to the code so it's easier to understand the problem. 编辑:在代码中添加了一些console.log,以便更容易理解问题。 Also solved wrong variable name in the description. 还在说明中解决了错误的变量名称。

I expect your problem here is with getParams . 我希望你的问题在于getParams As written, getParams will return an object where the bioinformatics is passed by reference. 如上所述, getParams将返回一个通过引用传递生物信息学的对象。 In other words, params.bioinformatics and data.bioinformatics are pointers to the same array. 换句话说, params.bioinformaticsdata.bioinformatics是指向同一个数组的指针。 So, when you modify params.bioinformatics , you see the behavior you are logging. 因此,当您修改params.bioinformatics ,您会看到您正在记录的行为。 Instead, you might get your bioinformatics property as a copy rather than a reference. 相反,您可能会将生物信息学属性视为副本而非参考。

getParams: function () {
  return {
    organism:           this.organism,
    species_id:         this.speciesID,
    bioinformatics:     this.bioinformatics.slice(),
    // ... more params
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM