传入一个对象的所有 property
作者:互联网
如果你想要将一个对象的所有 property 都作为 prop 传入,你可以使用不带参数的
v-bind
(取代v-bind:prop-name
)。例如,对于一个给定的对象post
:
post: {
id: 1,
title: 'My Journey with Vue'
}
下面的模板:
<blog-post v-bind="post"></blog-post>
等价于:
<blog-post
v-bind:id="post.id"
v-bind:title="post.title"
></blog-post>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>传入一个对象的所有 property</title>
<script src="./vue.js"></script>
</head>
<body>
<div id="hzh">
<hzh-component
v-for="post in posts"
v-bind:post="post"
></hzh-component>
</div>
<script>
Vue.component('hzh-component', {
props: ['post'],
template: `
<div class="hzh-component">
<h3>作者的名字:{{post.author.name}}</h3>
<h3>作者的弟弟:{{post.author.brother}}</h3>
<h3>作者的妈妈:{{post.author.mother}}</h3>
</div>
`
})
new Vue({
el: '#hzh',
data: {
posts: [
{ key: 1,
author: {
name: '黄子涵',
brother: '黄春钦',
mother: '陈兰英'
}
}
]
}
})
</script>
</body>
标签:Vue,name,author,对象,传入,hzh,post,property 来源: https://www.cnblogs.com/Huang-zihan/p/16343801.html