父组件
给子组件传递iconName和placeholder。
<LoginInput :iconName="'user'" class="inputStyle" ref="user" :placeholder="'请输入用户名'"/>
<LoginInput :iconName="'pwd'" class="inputStyle" style="margin-bottom: 15px;" ref="pwd" :placeholder="'请输入密码'"/>
子组件
通过计算属性对iconName进行判断选取哪个背景图,再进行绑定style。
<input :style="{backgroundImage:'url('+ iconUrl +')'}" type="text" name="" id="" v-model:value="value"
:placeholder="placeholder" class="userInput" />
export default {
data() {
return {
userIcon: require('../static/icons/user.png'),
pwdIcon: require('../static/icons/password.png'),
value: ''
}
},
methods: {
getValue() {
return this.value
}
},
props: {
iconName: {
default: "",
type: String
},
placeholder: {
default: "",
type: String
}
},
computed: {
iconUrl() {
switch (this.iconName) {
case 'user':
return this.userIcon
case 'pwd':
return this.pwdIcon
default:
return ''
}
}
}
}