【发布时间】:2017-12-23 18:53:40
【问题描述】:
我想知道如何使用道具和检索将对象传递给子组件。我了解如何将其作为属性来执行,但如何传递对象并从子组件中检索对象?当我从子组件中使用 this.props 时,我得到未定义或错误消息。
父组件
<template>
<div>
<child-component :v-bind="props"></child-component>
</div>
</template>
<script>
import ChildComponent from "ChildComponent.vue";
export default {
name: 'ParentComponent',
mounted() {
},
props: {
firstname: 'UserFirstName',
lastname: 'UserLastName'
foo:'bar'
},
components: {
ChildComponent
},
methods: {
}
}
</script>
<style scoped>
</style>
子组件
<script>
<template>
<div>
</div>
</template>
export default {
name: 'ChildComponent',
mounted() {
console.log(this.props)
}
}
</script>
【问题讨论】:
-
1) 您没有在父组件上正确定义道具。 2)您不能通过
v-bind="props"传递父组件的道具。 3) 如果您通过v-bind将道具值的对象传递给子组件,您仍然需要在子组件中定义这些道具。 4) 从子组件“检索”数据是通过事件完成的。 5) 请阅读Vue components上的文档 -
我不需要使用 v-bind。我从一个例子中得到了它并尝试使用它。我正在寻找实现这一目标的正确方法。我现在正在查看 Vue 组件文档
标签: components vuejs2