Vue组件通讯之子传父

子传父的通讯方式的实现是通过订阅者模式来实现的

<!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>Aui车养护</title>
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>

<body>
    <div id="app">
        <div :style="{ fontSize: postFontSize + 'em' }">
            <blog-post v-for="post in posts" v-bind:key="post.id" v-bind:post="post" v-on:enlarge-text="change"></blog-post>
        </div>
    </div>
    <script>
        Vue.component('blog-post', {
            props: {
                post: {
                    type: Object,
                    required: true,
                },
            },
            template:
                `
                <div class="blog-post">
                    <h3  v-on:click="changeColor" :style="{color:activeColor;}"> {{ post.title }}</h3 >
                    <button v-on:click="$emit('enlarge-text',0.1)">
                        Enlarge text
                    </button>
                    <div v-html="post.content"></div>
                </div > 
           `,
            methods: {
                changeColor() {
                    let colors = ['red', 'blue', 'green', 'lightgreen'],
                        i = 0;
                    console.log("子组件的点击事件");
                    if (i > colors.length) i = 0;
                    let colorflag = colors[i];
                }
            },
            data: {
                activeColor: 'red',
            }
        })
        let vm = new Vue({
            el: "#app",
            data: {
                a: 1,
                posts: [
                    { id: 1, title: 'My journey with Vue' },
                    { id: 2, title: 'Blogging with Vue' },
                    { id: 3, title: 'Why Vue is so fun' }
                ],
                postFontSize: 1,
                blog: "击鼓传花的游戏",
            },
            created() {
                console.log('this = ', this);
            },
            methods: {
                change(enlargeAmount) {
                    this.postFontSize += enlargeAmount;
                }
            }
        });
    </script>
</body>

</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 中,父组件和子组件之间可以通过事件通信。 父组件向子组件传递数据,可以通过 props 属性实现。而子组件向父组件传递数据,则可以通过事件的方式实现。 具体实现方法如下: 1. 父组件向子组件传递数据 在父组件中,通过 props 属性将数据传递给子组件: ```html <template> <div> <child-component :message="message"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue' export default { components: { ChildComponent }, data() { return { message: 'Hello world!' } } } </script> ``` 在子组件中,接收父组件传递的数据: ```html <template> <div>{{ message }}</div> </template> <script> export default { props: { message: { type: String, default: '' } } } </script> ``` 2. 子组件向父组件传递数据 在子组件中,通过 $emit() 方法触发事件,将数据传递给父组件: ```html <template> <button @click="sendMessage">发送消息</button> </template> <script> export default { methods: { sendMessage() { this.$emit('send-message', 'Hello world!') } } } </script> ``` 在父组件中,通过 v-on 指令监听子组件触发的事件,并在事件处理函数中获取子组件传递的数据: ```html <template> <div> <child-component @send-message="handleMessage"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue' export default { components: { ChildComponent }, methods: { handleMessage(message) { console.log(message) // 输出:Hello world! } } } </script> ``` 以上就是 Vue 中父组件和子组件之间通过事件通信的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值