vue自定义组件用v-model来接受子组件的值,可以自定义任何样式输入框组件

今天教大家一个有趣的操作:

如图:利用v-model属性接受组件值,实现自定义样式输入框
在这里插入图片描述

父组件代码:

<template>
    <div class="logon">
   	 <Input type="number"
            v-model="abcd"
            placeholder="请输入手机号"/>
            {{ abcd }}
    </div>
</template>

<script>
    import Input from "../../components/logon/Input";
	 export default {
        name: "logon",
         components: {
            Input,
        },
        data() {
            return {
                abcd:''
            }
        },
    }
</script>

**子组件:**重点在派发 this.$emit('input',this.value) 这里是触发父组件input事件

<template>
    <div class="input-label" ref="inputLabel">
        <label>
            <input class="inp"
                   v-model="value"
                   :type="type"
                   :placeholder="placeholder"
                   @input="inputs"
                   @focus="change"
                   @blur="blur">
        </label>
    </div>
</template>

<script>
    export default {
        name: "Input",
        props: {
            type: {
                default: 'text',
                type: String
            },
            placeholder: {
                default: "请输入",
                type: String
            },
        },
        data() {
            return {
                value: ''
            }
        },
        methods: {
            change() {
                this.$refs.inputLabel.style = "border-bottom: 1px solid #FE792A"
            },
            blur() {
                this.$refs.inputLabel.style = "border-bottom: 1px solid #eee"
            },
            inputs() {
                this.$emit('input',this.value)
            }
        }
    }
</script>

<style scoped lang="scss">
    .input-label {
        border-bottom: .6px solid #eee;
        width: 80%;
        margin: 0 auto;


        .inp {
            width: 100%;
            border: none;
            height: 60px;
            margin: 20px 0;
            font-size: 4vw;
            caret-color: #616161;

            &::placeholder {
                color: rgba(153, 153, 153, 0.87);
            }
        }
    }
</style>

你学到了吗!😊

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值