【发布时间】:2020-04-30 16:49:04
【问题描述】:
在使用 v-for 时将 Vue 道具传递给子组件时,子组件出现在页面上但不显示该道具。浏览器控制台中没有错误。任何帮助将不胜感激。
传递prop的组件:
<template>
<div class="col" ref="participants-window">
<div>
<user
v-for="(username, index) in participants"
v-bind:username="username"
v-bind:index="index"
> </user>
</div>
</div>
</template>
<script>
export default {
props: ['participants'],
data() {
return {
show: true,
username: null
}
},
mounted() {
this.scrollToBottom();
},
watch: {
messages() {
this.scrollToBottom();
}
},
methods: {
scrollToBottom() {
this.$nextTick(() => {
this.$refs['participants-window'].scrollTop = this.$refs['participants-window'].scrollHeight;
});
}
},
}
</script>
<style scoped>
.col {
overflow-y: auto;
max-height: 200px;
word-wrap: break-word;
}
</style>
接收道具的组件:
<template>
<div class="text-center">
<b-button id="tooltip-button-2" variant="primary">{{ username }}</b-button>
<b-tooltip show target="tooltip-button-2">tooltip</b-tooltip>
</div>
</template>
<script>
props: ['username']
export default {
data() {
return {
show: true,
username: null
}
}
}
</script>
【问题讨论】:
-
使用
v-bind:key="index"作为user的道具