Skip to content

Commit 38b8940

Browse files
committed
儿子向父亲传递数据
1 parent 65752ea commit 38b8940

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

vuejs入门基础/my-second-vue-project/src/App.vue

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
{{item.label}}
88
</li>
99
</ul>
10-
<component-a msgfromfather='you die!'></component-a>
10+
<p>child tells me: {{childWords}} + {{childWordstow}}</p>
11+
<component-a msgfromfather='you die!' v-on:child-tell-me-something='listenToMyBoy'></component-a>
1112
</div>
1213
</template>
1314

@@ -19,7 +20,9 @@
1920
return {
2021
title: 'this is a todo list',
2122
items: Store.fetch(),
22-
newItem: ''
23+
newItem: '',
24+
childWords:'',
25+
childWordstow:''
2326
};
2427
},
2528
components:{ComponentA},
@@ -31,6 +34,11 @@
3134
deep:true
3235
}
3336
},
37+
events:{
38+
'child-tell-me-something-two':function (msg) {
39+
this.childWordstow = msg;
40+
}
41+
},
3442
methods: {
3543
toggleFinish: function (item) {
3644
item.isFinished = !item.isFinished;
@@ -42,6 +50,9 @@
4250
isFinished: false,
4351
});
4452
this.newItem = '';
53+
},
54+
listenToMyBoy:function(msg){
55+
this.childWords = msg;
4556
}
4657
}
4758
}

vuejs入门基础/my-second-vue-project/src/components/componentA.vue

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,29 @@
22
<div class="hello">
33
<h1>{{ msg }}</h1>
44
<h1>{{msgfromfather}}</h1>
5-
<button v-on:click="onClickMe">Click!</button>
5+
<button v-on:click="onClickMe">open mouse!</button>
6+
<button v-on:click="onClickMetwo">open mouse!</button>
67
</div>
78
</template>
89

910
<script>
1011
export default {
1112
data () {
1213
return {
13-
msg: 'hello from component a!'
14+
msg: 'hello from component a!',
15+
msg1:'dispatch msg'
1416
}
1517
},
1618
props:['msgfromfather'],
1719
methods:{
1820
onClickMe:function(){
19-
console.log(this.msgfromfather);
20-
}
21+
console.log(this.msgfromfather);
22+
this.$emit('child-tell-me-something',this.msg);
23+
},
24+
onClickMetwo:function(){
25+
console.log(this.msgfromfather);
26+
this.$dispatch('child-tell-me-something-two',this.msg1);
27+
}
2128
}
2229
}
2330
</script>

0 commit comments

Comments
 (0)