0
如何將由組件內的方法創建的對象數組傳遞給另一個組件?我嘗試過使用道具,但也許我對道具的工作原理是不正確的。在組件之間傳遞數據時遇到問題
<template>
<div class="media-body">
<ul>
<li v-for="item in items" v-bind:class="{ active: item.active }">{{ item.text }}
</li>
</ul>
<button type="button" class="btn btn-danger" v-on:click="addItem">Test</button>
</template>
<script>
export default {
data() {
return {
item: "a",
item2: "b",
item3: "c",
item4: "d",
items: [],
}
},
methods: {
addItem: function() {
var testArray = this.item.concat(this.item2, this.item3, this.item4);
for (var i = 0; i < testArray.length; i++) {
this.items.push({
text: testArray[i],
active: false });
}
// if I check console.log(this.items) at this point I can see the data I want to pass
},
}
}
</script>
輔助組件我嘗試將數據傳遞給。
<template>
<div class="media-body">
<div class="media-label">Title:
<textarea class="form-control" placeholder="Title"></textarea>
</div>
</div>
</template>
<script>
export default {
props: ['items'],
data() {
return {
}
},
</script>
如果我做的console.log(項目)我得到 「optimizerTitle.vue FAEE:36未捕獲的ReferenceError:項目沒有定義」 如果我做的console.log(this.items)我得到一個未定義錯誤。任何想法爲什麼? – Pyreal