Vue.JS如何在组件循环中访问对象属性

时间:2017-12-22 02:11:18

标签: vue.js

我有一个项目列表作为组件:

Vue.component('building-list', {

    template: `<div><building v-for="building in buildings">{{ building.id }}</building></div>`,

    data() {

        return {

                buildings: {

                    'scavenger': { id: 'scavenger', name: "desh scavenger", amount_owned: 0, cost: 10, base_cps: 1, description: "A scavenger, specialising in the aquisition of Desh" },
                    'vaporator': { id: 'vaporator', name: "moisture farm", amount_owned: 0, cost: 50, base_cps: 2 },
                    'cantina': { id: 'cantina', name: "cantina", amount_owned: 0, cost: 650, base_cps: 3 },
                    'bazaar': { id: 'bazaar', name: "bazaar", amount_owned: 0, cost: 7800, base_cps: 4 },
                    'droidshop': { id: 'droidshop', name: "droid workshop", amount_owned: 0, cost: 80000, base_cps: 5 },
                    'bountyhunter': { id: 'bountyhunter', name: "bounty hunter guild", amount_owned: 0, cost: 140000, base_cps: 6 },
                    'kybermine': { id: 'kybermine', name: "kyber crystal mine", amount_owned: 0, cost: 250000, base_cps: 7 }

                }
        }

    }

});

我希望看起来像这样的每一项:

Vue.component('building', {

    template: `

        <div :key="building[id]" class="card" style="display: block;" v-on:click="buy_building(building[id])"> 
            <img :src="building[id]" style="cursor: pointer; width:100%; height:145px;">
            <div class="card-block">
                <h4 class="card-title">{{ building[name] }}</h4>
                <p class="card-text">cost: {{ building[cost] }}</p>
                <p class="card-text">owned: {{ building[amount_owned] }}</p>
            </div>
        </div>`

});

        <div class="container-fluid">

            <building-list></building-list>

        </div>

当我尝试运行此项时,我会为每个项目收到此错误:

"Property or method "building" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties."

我是Vue的新手,所以我按照文档的链接进行了操作,但是它没有真正解释如何以这种特殊方式执行此操作,从我一直关注的教程中看,这看起来是对的,但是,那些教程并没有真正解释这种情况。

这是否是正确的做法?

1 个答案:

答案 0 :(得分:3)

  1. 您必须使用props将父组件的传递数据传递给子组件。
  2. 您可以使用'点表示法'来访问对象属性。如果要使用括号表示法访问,则应编写类似obj ['property']的字符串。 Read property accessors
  3. Vue.component('building', {
      props: ['building'],
      template: `
            <div :key="building.id" class="card" style="display: block;" v-on:click="buy_building(building.id)">
                <div class="card-block">
                    <h4 class="card-title">{{ building.name }}</h4>
                    <p class="card-text">cost: {{ building.cost }}</p>
                    <p class="card-text">owned: {{ building.amount_owned }}</p>
                </div>
            </div>
            `,
      methods: {
        buy_building(itemId) {
          alert(itemId);
        }
      }
    });
    
    new Vue({
      el: 'building-list',
      template: `<div><building v-for="item in buildings" :building='item' :key='item.id'></building></div>`,
      data() {
    
        return {
    
          buildings: {
            'scavenger': {
              id: 'scavenger',
              name: "desh scavenger",
              amount_owned: 0,
              cost: 10,
              base_cps: 1,
              description: "A scavenger, specialising in the aquisition of Desh"
            },
            'vaporator': {
              id: 'vaporator',
              name: "moisture farm",
              amount_owned: 0,
              cost: 50,
              base_cps: 2
            },
            'cantina': {
              id: 'cantina',
              name: "cantina",
              amount_owned: 0,
              cost: 650,
              base_cps: 3
            },
            'bazaar': {
              id: 'bazaar',
              name: "bazaar",
              amount_owned: 0,
              cost: 7800,
              base_cps: 4
            },
            'droidshop': {
              id: 'droidshop',
              name: "droid workshop",
              amount_owned: 0,
              cost: 80000,
              base_cps: 5
            },
            'bountyhunter': {
              id: 'bountyhunter',
              name: "bounty hunter guild",
              amount_owned: 0,
              cost: 140000,
              base_cps: 6
            },
            'kybermine': {
              id: 'kybermine',
              name: "kyber crystal mine",
              amount_owned: 0,
              cost: 250000,
              base_cps: 7
            }
    
          }
        }
    
      }
    
    });
        .card {
          outline: 1px solid aquamarine;
          width: 200px;
        }
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    <div class="container-fluid">
      <building-list></building-list>
    </div>