开发者问题收集

Vuejs:v-for,无法读取未定义的属性

2020-07-16
1696

我尝试根据包含问题和答案的对象列表制作几个常见问题框。但我收到“无法读取未定义的属性‘问题’”的提示,为什么?我的代码如下所示:

export default {
  data() {
    return {
      totalFAQs: [{
          id: '1',
          question: 'This is a question',
          answer: 'This is an answer to your question'
        },
        {
          id: '2',
          question: 'This is a question',
          answer: 'This is an answer to your question'
        }
      ]
    }
  }
}
<div :v-for="faq in totalFAQs" :key="item.id" class="section-container">
  <div class="faq-question">
    <h4>{{faq.question}}</h4>
  </div>
  <div class="expanded-content expanded-faq">
    <div class="faq-answer">
      <p>{{faq.answer}}</p>
    </div>
  </div>
</div>
2个回答

我认为这个问题与您将 key 值设置为 item.id 而不是 faq.id 有关。另外, v-for 开头不应写 :

holmicz
2020-07-16

您需要将“:v-for”更改为此“v-for”,这是正确的语法。在您的关键部分中,您有错误的变量

 <div v-for="faq in totalFAQs" :key="faq.id"class="section-container">
      <div class="faq-question">
        <h4>{{faq.question}}</h4>
      </div>
      <div class="expanded-content expanded-faq">
        <div class="faq-answer">
          <p>{{faq.answer}}</p>
        </div>
      </div>
    </div>
Omar Sy
2020-07-16