vue组件传值(子父、父子、兄弟)

vue组件传值(子父、父子、兄弟)

vue组件传值,分三类:
1.父 ---->子 
2.子—>父
3.兄弟—>兄弟

1.父向子传值

用props,3步解决:

  1. 在子组件props属性中随便取个名字,我取了cvalue
    全新的界面设计** ,将会带来全新的写作体验;

  2. 在父组件中引入子组件,将刚才取的随便的名字绑定个值,这个值名字(我取了value)为父组件的data
    全新的界面设计** ,将会带来全新的写作体验;
    这个是父组件的data的value的值
    在这里插入图片描述

  3. 绑定好了就可以肆意的在子组件中使用啦,这里cvalue的值就是父组件data中value的值

  4. 在这里插入图片描述
    附上例子代码:

  <!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">
    <!-- vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
    <title>Document</title>
</head>
<body>
    <div id='father'>
        <child :cvalue="value"></child>
    </div>
</body>

<template id='child'>
    <h1>{{cvalue}}</h1>
</template>

<script>
    var vm=new Vue({
        el:"#father",
        data:{
            value:'这是父组件的data'
        },
        components:{
            child: {
                template:"#child", 
                props:["cvalue"]
            }
        }  
    })

</script>
</html>

2.子向父传值

用$emit,3步解决:

  1. 在子组件创建个方法,用$emit(事件名称,要传递的值)注册事件.ps:可以理解为何click一样,触发时执行定义的函数
    在这里插入图片描述
  2. 在子组件定义什么时候触发事件,这里我写的是当点击h1的时候tofather事件被触发
    . 在这里插入图片描述
  3. 在父组件对tofather事件进行绑定,触发的时候要干啥(我这里调用了个方法,把触发时的值this.cvalue作为参数传了进去,注意!!!参数必须写 e v e n t , 因 为 事 件 响 应 传 过 来 的 值 是 用 event,因为事件响应传过来的值是用 eventevent接收滴)
    在这里插入图片描述
    下图为我定义的父组件getvalue的方法,这里的形参可以随便取:
    在这里插入图片描述
    附上例子代码:
 <!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">
    <!-- vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
    <title>Document</title>
</head>
<body>
    <div id='father'>
        <child @tofather='getvalue($event)'></child>
    </div>

    <template id='child'>
            <h1 @click="onclick">这个是子组件{{cvalue}}</h1>
    </template>
</body>

</html>

<script>
    var vm=new Vue({
        el:"#father",
        data:{
            value:'这是父组件的data'
        },
        methods:{
            getvalue($event){
                alert('这个是子组件传递过来的值:'+$event)
            }

        },
        components:{
            child: {
                template:"#child", 
                data(){
                    return{
                        cvalue:'这是子组件的值'
                    }
                },
               methods: {
                  onclick(){
                     
                       this.$emit('tofather',this.cvalue)
                   }
               },
              
            }
        }  
    })

</script>

3.兄弟传值(子子传值)

方法1:先子传父,再父传子(小型项目可以这么搞)

方法2:用vuex,3步解决;
(ps:vuex相当于一个仓库,或者说是数据库,创建之后你可以随时的对这个仓库里的数据进行改查!
vuex:4个属性:state:存放数据;getters:查询数据;mutation:修改数据;action:异步运行mutation.)

  1. 创建vuex仓库:
    2.
  2. 组件1里要传的值放入仓库,我把data中的msg放入仓库,记得声明sotre属性,第二个store为所创建的Vuex.Store实例,我定义了个方法,绑定了个点击事件,调用方法

在这里插入图片描述
当点击h2时调用senMsg()把仓库里msg的值设置为组件1中的msg的值
在这里插入图片描述
3. 组件2中把仓库中的msg的值设置为计算属性,当上面点击h2时,组件1修改仓库里msg的值,组件2获取到仓库中msg的值,就完成了组件1向组件2传值
在这里插入图片描述
附上例子代码:

<!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">
    <!-- vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
    <script src="https://cdn.bootcss.com/vuex/3.0.1/vuex.min.js"></script>
    <title>Document</title>
</head>
<body>
    <div id='father'>
        <syb1></syb1>
        <syb2></syb2>
    </div>
</body>
<template id='syb1'>
    <div>
     <h1>这是兄弟1</h1>
     <h2 @click="sendMsg">{{msg}}</h2>
    </div>   
</template>

<template id='syb2'>
    <div>
            <h1 >这是兄弟2</h1>
            <h1>{{getMsg}}</h1>
    </div>
        
</template>

<script>
    var store = new Vuex.Store({
        state:{
            msg:'这个是存在vuex里的值:'

        },
        getters:{
            getMsg(state){
                return state.msg
            }

        },
        mutations:{
            setMsg(state,msg){
                state.msg+=msg
            }

        },
        actions:{
            setMsg(content){
                content.commit(setMsg,msg)
            }

        }

    })

    var vm=new Vue({
        el:"#father",
        data:{
            value:'这是父组件的data',
            
        },
        created() {
            console.log(this.$store)
        },
        components:{
            syb1: {
                template:"#syb1", 
                data(){
                    return {
                        msg:'这是兄弟1的消息'
                    }
                },
                methods: {
                    sendMsg(){
                        this.$store.commit('setMsg',this.msg)
                    }
                },
                store:store
               
            },
            syb2: {
                template:"#syb2", 
                data(){
                    return {
                    }
                },
                computed: {
                    getMsg(){
                       return this.$store.state.msg
                    }
                },
                store:store
               
            }
        }  
    })
</script>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值