Vue 父组件向子组件传递对象
传递参数:v-bind:author="author"<body><div id="app"><blog-post v-bind:author="author"></blog-post></div><script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"&...
·
传递参数:v-bind:author="author"
,父组件 blog-post
将对象 author 以传递对象的方式传入子组件
<body>
<div id="app">
<blog-post v-bind:author="author"></blog-post>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
Vue.component('blog-post', {
props: ['author'],
template: '<div>{{author.name}}--{{author.age}}--{{author.male}}</div>'
})
const vm = new Vue({
el: '#app',
data: {
author: {
name: 'Jerry',
age: 18,
male: 'male'
}
}
})
</script>
</body>
输出结果:
传递属性:v-bind="author"
,父组件 blog-post
将对象 author 以传递属性的方式传入子组件
<body>
<div id="app">
<blog-post v-bind="author"></blog-post>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
Vue.component('blog-post', {
props: [],
template: '<div></div>'
})
const vm = new Vue({
el: '#app',
data: {
author: {
name: 'Jerry',
age: 18,
male: 'male'
}
}
})
</script>
</body>
输出结果:
在子组件中接收父组件的 name、age、male 属性
<body>
<div id="app">
<blog-post v-bind="author"></blog-post>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
Vue.component('blog-post', {
props: ['name', 'age', 'male'],
template: '<div>{{name}}--{{age}}--{{male}}</div>'
})
const vm = new Vue({
el: '#app',
data: {
author: {
name: 'Jerry',
age: 18,
male: 'male'
}
}
})
</script>
</body>
结论:
- 父组件向组件件中传递对象,可以采用绑定属性的方式,简化传递参数的过程。
更多推荐
已为社区贡献1条内容
所有评论(0)