【发布时间】:2020-10-08 01:39:54
【问题描述】:
我想根据道具类型在我的输入中显示和隐藏一个图标。
如果类型是密码,它将显示使密码可见/隐藏的图标;如果类型不是密码,它应该隐藏将 v-show 更改为 false 的图标。
这是带有 v-show showPwIcon 以使图标可见的 HTML
<input
ref="textField"
v-model="textField"
:placeholder="placeholder"
:type="type"
>
<span v-show="showPwIcon" @click="switchVisibility">
<i :class="[pwIcon ? 'ri-eye-line' : 'ri-eye-off-line']"></i>
</span>
这里是 JS,没有 CSS,仅作为示例,v-show showPwIcon 默认设置为 false:
export default {
props:{
placeholder: {
type: String,
value: ''
},
type: {
type: String,
value: '',
required: true
}
},
data() {
return {
textField: '',
showPwIcon: false,
pwIcon: true
}
},
watch: {
type: function (password) {
const inputType = this.$refs.textField.type
if (inputType === 'password') {
this.showPwIcon = true
} else {
this.showPwIcon = false
}
}
},
methods: {
switchVisibility() {
this.$emit('input', {
type: this.$refs.textField.type = this.$refs.textField.type === 'password' ? 'text' : 'password'
})
if (this.$refs.textField.type === 'password') {
this.pwIcon = true
} else {
this.pwIcon = false
}
}
}
}
使用当前代码我遇到了这个问题:
输入文字不显示图标
<text-field-icon
placeholder="Placeholder here"
type="text"
/>
类型是密码但是v-show不是真的。
<text-field-icon
placeholder="Placeholder here"
type="password"
/>
我正在尝试通过查看输入上的道具类型来完成这项工作,但我无法通过this.$refs.textField.type 获取输入类型
示例代码https://codesandbox.io/s/vue-tailwind-template-dteq4
有什么想法吗?
【问题讨论】:
-
请为此使用codesandbox.io 创建一个small demo 以显示正在发生的问题。
-
已更新代码示例
标签: javascript vue.js