实现方式是在
el-input__wrapper节点中直接appendChild一个新节点。
主要代码:
const { proxy } = getCurrentInstance()
onMounted(() => {
nextTick(() => {
let unitNode = null
watch(() => props.unit, (val) => {
if (!val) {
if (unitNode) {
unitNode.remove()
}
} else {
const input__wrapper = proxy.$el.getElementsByClassName('el-input__wrapper')
if (input__wrapper.length) {
if (!unitNode) {
unitNode = document.createElement('span')
unitNode.className = 'number-unit'
input__wrapper[0].appendChild(unitNode)
}
unitNode.innerHTML = val
}
}
}, { immediate: true })
})
})
可选样式:
.number-unit{
padding-left: 8px;
color: #999;
white-space: nowrap;
}
文章描述了如何在Vue组件的el-input__wrapper中使用watch和DOM操作来动态插入或移除单位标签,当props.unit值变化时更新单位显示。
5463

被折叠的 条评论
为什么被折叠?



