代码之家  ›  专栏  ›  技术社区  ›  mheavers

通过数组索引将对象从数据传递到vue.js中的组件

  •  0
  • mheavers  · 技术社区  · 5 年前

    我的父应用程序具有以下数据结构

    export default {
          name: "app",
          components: {
            Template1
          },
          data() {
              appState: {
                questionCount: 0,
                isIntro: true, //when true, perform single initial animation
              },
              questions: [
                {
                  id: 0,
                  question: "What do you think is most helpful in increasing workforce diversity?",
                }
                ...
              ]
          }
        }
    

    我希望仅使用数据中的question对象呈现template1,该对象等于当前问题计数,因此我尝试了以下操作:

    <template>
      <div id="app">
        <Template1
          questionItem="questions[appState.questionCount]"
        />
      </div>
    </template>
    

    但我不认为这是引用数组索引的正确方法。正确的方法是什么?

    1 回复  |  直到 5 年前
        1
  •  0
  •   Boussadjra Brahim    5 年前

    你应该用 v-bind: 或者只是 : 如下:

    <template>
      <div id="app">
        <Template1
          :questionItem="questions[appState.questionCount]"
        />
      </div>
    </template