<!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>
03_02.子傳父
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。