03_02.子传父
2017-11-14 本文已影响0人
Robyn_Luo
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<main id="app">
<!-- 1 父使用子的时候, 监听子事件 -->
<app-header v-bind:tit="title" v-on:changecount="change"></app-header>
</main>
<script src="vue.js"></script>
<script>
Vue.component('app-header', {
template: `<header>{{ tit }}{{ count }}<button @click="countAdd">点我</button></header>`,
data: function() {
return {
count: 0
};
},
props: ['tit'],
methods: {
countAdd: function() {
this.count++;
this.$emit('changecount', this.count); // 2 子在需要的时候触发事件
}
}
});
var vm = new Vue({
el: '#app',
data: {
title: '登陆'
},
methods: {
change: function(val) {
console.log(`接收来自子的值:${val}`);
}
}
});
</script>
</body>
</html>