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

Vue。JS如何访问组件循环中的对象属性

  •  2
  • Aaranihlus  · 技术社区  · 8 年前

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

    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 回复  |  直到 8 年前
        1
  •  4
  •   Sergey    8 年前
    1. 您必须使用 props 用于将数据从父组件传递到子组件。
    2. 可以使用“点符号”访问对象属性。如果希望使用括号表示法访问,则应编写类似obj['property']的字符串。 Read property accessors

    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>
    推荐文章