今天教大家一个有趣的操作:
如图:利用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>