组件通信-父子组件的案例

父传子 

<body>
    <div id="app">
        <cpn :cnum1="num1" :cnum2="num2"></cpn>

    </div>
    <template id="cpn">
        <div>
            <h2>props:{{cnum1}}</h2>
            <h2>data:{{dnumber1}}</h2>
            <input type="text" v-model="dnumber1">
            <h2>props:{{cnum2}}</h2>
            <h2>data:{{dnumber2}}</h2>
            <input type="text" v-model="dnumber2">

        </div>
    </template>


    <script src="../vue.js"></script>
    <script>
        const cpn = {
            template: '#cpn',
            // 对象
            props: {
                cnum1: Number,
                cnum2: Number
            },
            // 组价中的data是函数,不是对象
            data() {
                return {
                    dnumber1: this.cnum1,
                    dnumber2: this.cnum2
                }
            }
        }
        const app = new Vue({
            el: "#app",
            data: {
                num1: 1,
                num2: 2
            },
            components: {
                cpn
            }
        })
    </script>
</body>

 

 

父传子和子传父双向绑定

<body>
    <div id="app">
        <cpn :cnum1="num1" :cnum2="num2" @num1change="num1change"  @num2change="num2change"></cpn>

    </div>
    <template id="cpn">
        <div>
            <h2>props:{{cnum1}}</h2>
            <h2>data:{{dnumber1}}</h2>
            <!-- <input type="text" v-model="dnumber1"> -->
            <input type="text" :value="dnumber1" @input="num1Input">
            <h2>props:{{cnum2}}</h2>
            <h2>data:{{dnumber2}}</h2>
            <!-- <input type="text" v-model="dnumber2"> -->
            <input type="text" :value="dnumber2" @input="num2Input">


        </div>
    </template>


    <script src="../vue.js"></script>
    <script>
        const cpn = {
            template: '#cpn',
            // 对象
            props: {
                cnum1: Number,
                cnum2: Number
            },
            // 组价中的data是函数,不是对象
            data() {
                return {
                    dnumber1: this.cnum1,
                    dnumber2: this.cnum2
                }
            },
            methods:{
                num1Input(event){
                    this.dnumber1=event.target.value;
                    this.$emit('num1change',this.dnumber1)

                },
                num2Input(event){
                    this.dnumber2=event.target.value
                    this.$emit('num2change',this.dnumber2)

                },

            }
        }
        const app = new Vue({
            el: "#app",
            data: {
                num1: 1,
                num2: 2
            },
            components: {
                cpn
            },
            methods:{
                num1change(value){
                    // 把字符串转换为数字
                    // this.num1=value*1;
                    this.num1=parseInt(value);

                },
                num2change(value){
                    this.num2=parseInt(value);
                },
            }
        })
    </script>
</body>

终结版!!!

<body>
    <div id="app">
        <cpn :cnum1="num1" :cnum2="num2" @num1change="num1change"  @num2change="num2change"></cpn>

    </div>
    <template id="cpn">
        <div>
            <h2>props:{{cnum1}}</h2>
            <h2>data:{{dnumber1}}</h2>
            <!-- <input type="text" v-model="dnumber1"> -->
            <input type="text" :value="dnumber1" @input="num1Input">
            <h2>props:{{cnum2}}</h2>
            <h2>data:{{dnumber2}}</h2>
            <!-- <input type="text" v-model="dnumber2"> -->
            <input type="text" :value="dnumber2" @input="num2Input">


        </div>
    </template>


    <script src="../vue.js"></script>
    <script>
        const cpn = {
            template: '#cpn',
            // 对象
            props: {
                cnum1: Number,
                cnum2: Number
            },
            // 组价中的data是函数,不是对象
            data() {
                return {
                    dnumber1: this.cnum1,
                    dnumber2: this.cnum2
                }
            },
            methods:{
                num1Input(event){
                    // 1.将input中的value赋值到dnumber中
                    this.dnumber1=event.target.value;
                    // 2.为了让父组件可以修改值,发出一个事件
                    this.$emit('num1change',this.dnumber1)

                    // 同时修改dnumber1的值  下面是上面的一百倍
                    this.dnumber2=this.dnumber1*100;
                    this.$emit('num2change',this.dnumber2)

                },
                num2Input(event){
                    this.dnumber2=event.target.value
                    this.$emit('num2change',this.dnumber2)

                      // 同时修改dnumber1的值  下面是上面的一百倍
                    this.dnumber1=this.dnumber1/100;
                    this.$emit('num1change',this.dnumber1)

                },

            }
        }
        const app = new Vue({
            el: "#app",
            data: {
                num1: 1,
                num2: 2
            },
            components: {
                cpn
            },
            methods:{
                num1change(value){
                    // 把字符串转换为数字
                    // this.num1=value*1;
                    this.num1=parseFloat(value);

                },
                num2change(value){
                    this.num2=parseFloat(value);
                },
            }
        })
    </script>
</body>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值