vue3 关于 textarea 设置自动高度的问题

105 阅读1分钟
<textarea 
    ref="textareaEle" 
    class="editor-inputer" 
    rows="1" 
    placeholder="Any thoughts..." 
    :style="{width: width + 'px', height: height + 'px'}" 
    @input="getTextareaValue">
</textarea>
const textareaEle = ref(null)
let width = ref(672)
let height = ref(24) // 默认高度

function getTextareaValue() {
    const _ele = textareaEle.value; // 获取 textarea 元素
    let lines = _ele.value.match(/\n/g); // 获取 textarea 输入的内容
    if (lines != null) {
        height.value = (lines.length + 1) * 24
    }
}