我有一个项目列表作为一个组件:
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非常陌生,所以我按照文档的链接进行了操作,但是它并没有真正解释如何以这种特定的方式进行操作,从我一直遵循的教程来看,这看起来是对的,但是这些教程也没有真正解释这种情况。
这样做对吗?