03_02.子传父

<!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>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Robyn_Luo阅读 1,630评论 0 0
  • Robyn_Luo阅读 1,036评论 0 0
  • 小院幽兰发,仙姿倚步廊。 冰肌如皓月,玉骨似凝霜。 素色盈盈笑,芳颜淡淡香。 折枝贻倩女,梳扮鬓云妆。
    泓颖阅读 1,652评论 4 5
  • 窗口左上角和左下角位置: var x=typeofscreenLeft=="number"?screenLeft:...
    frankisbaby阅读 3,247评论 0 0
  • 我,什么都不是 只是 浩瀚大海中 一叶孤舟 无所谓方向 无所谓力量 随风而行 风止则停 海浪给我肩膀 让我可以 倚...
    Pegga大婶阅读 3,880评论 0 1

友情链接更多精彩内容