1.父组件传递数据形式一
1.父组件代码为:
<template>
<div id="app">
<Blog :post='post'></Blog>
</div>
</template>
<script>
import Blog from './components/Blog.vue'
export default {
name: 'App',
components:{
Blog
},
data(){
return{
post:[{
id:1,
name:'guan'
},{
id:2,
name:'Wang'
}]
}
}
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
2.子组件代码为:
<template>
<div id="Blog">
<ul>
<li v-for='item in post'>{{item.name}}</li>
</ul>
</div>
</template>
<script>
export default{
name:'Blog',
props:['post'],
data(){
return{
}
}
}
</script>
<style>
li{
list-style: none;
}
</style>
2.父组件传递数据形式二
1.父组件代码为:
<template>
<div id="app">
<Blog></Blog>
</div>
</template>
<script>
import Blog from './components/Blog.vue'
export default {
name: 'App',
components:{
Blog
},
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
2.子组件代码为:
<template>
<div id="Blog">
<ul>
<li v-for='item in post'>{{item.name}}</li>
</ul>
</div>
</template>
<script>
export default{
name:'Blog',
props:{
post:{
type:Array,
default:[{
id:1,
name:"Zhang"
}]
}
},
data(){
return{
}
}
}
</script>
<style>
li{
list-style: none;
}
</style>