【发布时间】: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)] } <br />- 你这里有没有}?不应该是{{ studies[(session.studyId)] }} <br /> -
抱歉 - 复制和粘贴功能已关闭,但没有丢失括号。将在帖子中修复。感谢您指出错字。
标签: javascript vue.js