【问题标题】:[Vue warn]: Error in render: "TypeError: Unable to get property 'Id' of undefined or null reference[Vue警告]:渲染错误:“TypeError:无法获取未定义或空引用的属性'Id'
【发布时间】:2019-05-19 21:04:12
【问题描述】:

用 chrome、firefox、opera 和 internet explorer 对其进行了测试 并且该错误仅发生在Firefox中。

我在 vue js 中有两个计算属性,它们几乎同时发生变化。
当我更改我的选择框时,两者都会被触发,因为当我在选择框中选择不同的选项时this.gekozenWinding 会被更改。

这是我的两个属性的代码:

kant() {
  if (this.gekozenWinding != null) {
    var id = this.haalID(this.windings, this.gekozenWinding);
    parseInt(id);
    if (id < 5 && id > 0) return true;
    else {
      return false;
    }
  }
},
graden() {
  if (this.gekozenWinding != null) {
    var id = this.haalID(this.windings, this.gekozenWinding);
    switch (id) {
      case "1":
        return "180"; break;
      case "2":
        return "0"; break;
      case "3":
        return "270"; break;
      case "4":
        return "90"; break;
      case "5":
        return "180"; break;
      case "6":
        return "0"; break;
      case "7":
        return "270"; break;
      case "8":
        return "90"; break;
    }
  } else {
    return "0";
  }
}

我的方法 haalID()

    haalID(lijst, item) {
  /*
  1.Filters out given item(string) from a lijst(array).
  2.Gets the ID from the filtered object.
  3. returns the ID
  */
  var id = lijst.filter(i => i.Descriptions[0].Description == item);
  id = id[0].Id;
  return id;
}

这是在我的模板中

          <div class="row">
        <div class="form-group col-9">
          <label>Winding</label>
          <select
            class="form-control"
            v-model="gekozenWinding"
          >
            <option
              v-for="winding in windings"
              v-bind:key="winding.Id"
            >{{winding.Descriptions[0].Description}}</option>
          </select>
        </div>
      </div>
      <div v-if="gekozenWinding != null && gekozenWinding != 'To determine'">
        <div class="parent1" v-if="kant ">
          <img id="fto1" src="../assets/buitenkant.png" alt>
          <img
            id="fto2"
            v-bind:style="{'transform': `rotate(${graden}deg)`}"
            src="../assets/A.png"
            alt
          >
        </div>
        <div class="parent2" v-else>
          <img id="fto3" src="../assets/Binnenkant.png" alt>
          <img
            id="fto4"
            v-bind:style="{'transform': `rotate(${graden}deg)`}"
            src="../assets/A.png"
            alt
          >
        </div>
      </div>

同样,这只发生在 Internet Explorer(使用 11)中,其他浏览器也可以正常工作。
我什至尝试多次更改我的代码,但无法让它在 IE 中工作

【问题讨论】:

    标签: javascript internet-explorer vue.js computed-properties


    【解决方案1】:

    这个错误可能在模板中。 (你的windingsId)吗?

    在选项的v-for 中将key 更改为$index

    <option v-for="(winding, $index) in windings"
        v-bind:key="$index">
    {{winding.Descriptions[0].Description}}</option>
    

    PS:不知道为什么您在其他浏览器中没有看到此错误..

    【讨论】:

    • 发现了问题,如果字符串是句子,它不会过滤,所以我从项目(字符串)和描述(属性)中删除了空格。
    【解决方案2】:

    IE11(或任何 IE)不支持箭头函数。

    caniuse.com/#search=arrow%20functions -

    您需要使用像Babel 这样的编译器,或者避免使用箭头函数。

    var id = lijst.filter(function (i) {
      return i.Descriptions[0].Description == item;
    });
    

    【讨论】:

    • 仍然只在 IE 中得到相同的错误。甚至尝试将方法代码 (haalID) 直接放在我的属性中。
    • 发现了问题,如果字符串是句子,它不会过滤,所以我从项目(字符串)和描述(属性)中删除了空格
    猜你喜欢
    • 2018-07-14
    • 2021-02-17
    • 2021-03-21
    • 2021-08-18
    • 2020-09-17
    • 2020-01-01
    • 1970-01-01
    • 2020-04-07
    • 2018-10-01
    相关资源
    最近更新 更多