VueCli Prop

VueCli Prop 父傳子

新增Child.vue

父為Home.vue
1.引入Child.vue
註冊
2.components: {
Child
},
3.在template 下新增
4.子組件prop寫好後,到父組往下傳值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//Home.vue
<template>
<div class="home">
<Child :interest="interest" :lists="lists"/>
</div>
</template>

<script>
import Child from './Child.vue'
export default {
name: 'Home',
components: {
Child
},
data () {
return {
msg: 'Welcome to Your Vue.js App',
interest:'游泳',
lists:[
{id:'001',todo:'去旅行'},
{id:'002',todo:'寫部落格'}
]
}
}
}
</script>

子組件 Child.vue

script新增
props : {
interest : {
type : String,
require : true
},
lists:{
type : Object,
require : true
}
}
template新增

1
2
3
4
5
6
<h2>興趣:{{ interest }}</h2>
<ul>
<li v-for="item in lists" :key="item">
{{item.todo}}
</li>
</ul>

子組件完整組件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//Child.vue
<template>
<div class="home">
<h2>興趣:{{ interest }}</h2>
<ul>
<li v-for="item in lists" :key="item">
{{item.todo}}
</li>
</ul>
</div>
</template>

<script>
export default {
name: 'HelloWorld',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
},
props : {
interest : {
type : String,
require : true
},
lists:{
type : Object,
require : true
}
}
}
</script>

GitHub VueCli prop 父傳子

參考資料
VueCli(Vue2) prop 父傳子