【问题标题】:VueJS v-for loop breaks when referencing other ID引用其他 ID 时 VueJS v-for 循环中断
【发布时间】:2018-06-13 17:23:22
【问题描述】:

对 Vue 来说相对较新,我正在尝试构建比小部件更大的东西,但我遇到了障碍。

我试图在另一个对象的 v-for 循环中引用一个对象。即,会话循环内的 studyID,因此我可以打印与之关联的研究的名称。

当我尝试访问循环中的整个研究对象时..它有效!

 <div v-for="session in sessions">
        {{ studies[(session.studyId)] }} <br />
        {{ session.interviewee }}
      </div>

让我在 html 中呈现

{ "id": 0, "name": "Usability", "description": "General usability tests", "createdDate": "12-31-2017" } 
Carol Danvers 

<div v-for="session in sessions">
            {{ studies[(session.studyId)].name }}
            {{ session.interviewee }}
          </div>

给我一​​个控制台错误,“TypeError: _vm.studies[session.studyId] is undefined”

当我对其进行硬编码时(即 studies[0].name),它呈现得很好,这让我觉得我错过了 VueJs 如何评估表达式的一些细微差别。 p>

下面的数据结构示例。

studies: [
    {
      'id' : 0,
      'name' : "Usability",
      'description' : 'General usability tests',
      'createdDate' : '12-31-2017',
      'script' : [
        { 'id': 0, 'string' : "Question 1: What do you see when you do X, Y or Z?" },
        { 'id': 1, 'string' : "Question 2: Where would you expect it to be?"},
        { 'id': 2, 'string' : "Question 3: Is there anything important you'd want us to know after all of this?"}
        ]
    },
    {
      'id' : 1,
      'name' : "Contextual Inquiry",
      'createdDate' : '12-31-2017',
      'description' : 'On-site interviews with our users!',
      'script' : [
          { 'id': 0, 'string' : "Question 1: What do you see when you do X, Y or Z?" },
          { 'id': 1, 'string' : "Question 2: Where would you expect it to be?"},
          { 'id': 2, 'string' : "Question 3: Is there anything important you'd want us to know after all of this?"}
        ]
    }
  ],
  sessions: [
    {
      'interviewee': 'Carol Danvers',
      'timestamp' : '',
      'studyId' : 0,
      'Notes': [
        {
          'relativeTimestamp' : 43,
          'qID:': 0,
          'authorID': '0',
          'noteText': 'This is a note',
          'tags' : [1]
        },
        {
          'relativeTimestamp' : 17,
          'qID:': 2,
          'authorID': '1',
          'noteText': 'This is another note',
          'tags' : [1, 0, 2]
        },
        {
          'relativeTimestamp' : 1,
          'qID:': 2,
          'authorID': '0',
          'noteText': 'This is this working yet?',
          'tags' : [2,5]
        },
        {
          'relativeTimestamp' : 458,
          'qID:': 3,
          'authorID': '1',
          'noteText': 'Oh hey, this is working!',
          'tags' : [0,1]
        }
      ]
    },
    {
      'interviewee': 'Peter Parker',
      'timestamp' : '',
      'studyId' : 1,
      'Notes': [ ]
    },
    {
      'interviewee': 'Bruce Banner',
      'timestamp' : '',
      'studyId' : 0,
      'Notes': [ ]
    },
    {}
  ],

【问题讨论】:

  • {{ studies[(session.studyId)] } &lt;br /&gt; - 你这里有没有}?不应该是{{ studies[(session.studyId)] }} &lt;br /&gt;
  • 抱歉 - 复制和粘贴功能已关闭,但没有丢失括号。将在帖子中修复。感谢您指出错字。

标签: javascript vue.js


【解决方案1】:

查看您的数据 - 我发现您在 sessions 中的最后一项没有 studyId 字段。
我相信它会导致问题,您应该以某种方式处理它,例如:

{{ studies[(session.studyId || 0)] }} <br />

<div v-for="session in sessions">
    <template v-if="!!session.studyId">
        {{ studies[(session.studyId)] }} <br />
        {{ session.interviewee }}
    </template>
</div>

或在您的组件中创建一个计算属性validSessions,您可以在其中过滤掉空会话,然后在您的模板中使用它,如下所示:

<div v-for="session in validSessions">
   ...
</div>

【讨论】:

  • 是的,做到了!谢谢!!
猜你喜欢
  • 1970-01-01
  • 2020-04-01
  • 2020-04-04
  • 2021-12-28
  • 2018-08-04
  • 2018-12-23
  • 2020-01-31
  • 1970-01-01
  • 2018-06-14
相关资源
最近更新 更多