一、本文内容
本文内容记录render常用的一些属性和方法的配置,以作参考
export default {
data() {
return {
modelValue: '',
key: 0,
};
},
render(h) {
return h('div', [
h('input', {
class: 'input',
attrs: {
type: 'text'
},
key: this.key,
props: {
value: this.modelValue,
showPassword: item.password || false,
},
style: { display: 'hidden' ? 'none' : ''},
on: {
input: (e) => {
this.modelValue = e.target.value;
}
},
ref: 'myInput'
}),
h('p', `输入的内容是: ${this.modelValue}`)
]);
}
};
二、插槽的使用
import Vue from 'vue';
import { h } from 'vue/dist/vue.esm.js'; // 确保从正确的路径导入 h 函数
import { ElCascader, ElTooltip } from 'element-ui'; // 假设你已经安装了 element-ui
export default {
components: {
ElCascader,
ElTooltip
},
data() {
return {
value: [],
list: [
// ... 你的选项列表
]
};
},
render(createElement) {
const scopedSlot = scope => {
const { data } = scope;
return h('el-tooltip', {
props: {
disabled: data.label.length < 12,
effect: 'dark',
content: data.label,
placement: 'right'
},
class: 'item'
}, [
h('span', [data.label])
]);
};
return h('el-cascader', {
props: {
value: this.value,
options: this.list
},
scopedSlots: {
default: scopedSlot
}
});
}
};
三、el-input 使用 autocomplete
亲测,成功
<script>
import { h, resolveComponent } from 'vue';
export default {
data() {
return {
inputValue: ''
};
},
render() {
const ElInput = resolveComponent('el-input');
return h(ElInput, {
modelValue: this.inputValue,
'onUpdate:modelValue': value => {
this.inputValue = value;
},
placeholder: '请输入内容',
// 这里不能直接设置 autocomplete,但可以通过 ref 或其他方式在 mounted 钩子中设置
// 一种替代方案是使用一个包装组件或指令,但在这里我们直接展示如何在渲染后设置
ref: 'myInput',
// 你可以尝试通过 scoped slot 或其他方式传递原生属性,但 el-input 并不直接支持
// 因此,我们在这里不直接设置 autocomplete,而是在后面通过 DOM 操作设置
}, {
// 这里是插槽内容,但 el-input 通常不需要插槽内容来设置 autocomplete
// 插槽主要用于自定义内容,如前缀、后缀等
});
},
mounted() {
// 在组件挂载后,通过 ref 获取到 el-input 组件的 DOM 元素,然后找到内部的 input 元素并设置 autocomplete
this.$nextTick(() => {
const inputElement = this.$refs.myInput.$el.querySelector('input');
if (inputElement) {
inputElement.setAttribute('autocomplete', 'off');
}
});
}
};
</script>
这样可能会有auto complete不生效的情况,配合readonly,解决问题
再有情况可以如下操作:
autocomplete="off" 不生效的解决方法_autocomplete="off"不生效-CSDN博客
再有情况可以如下操作:
最终方案:
el-input 的 showPassword 属性 没有值的时候为 false,此时type为 text,有值的时候为true,此时type为password。解决了。
参考链接
https://blog.csdn.net/qq_42277412/article/details/132872933