kbone icon indicating copy to clipboard operation
kbone copied to clipboard

`超级大BUG` `vue3` input 组件有问题

Open tesla-cat opened this issue 1 year ago • 2 comments

<script setup>
const val = defineModel()
</script>

<template>
  <div class="f1 col">
    <div>{{ { type: typeof val, val: val.slice(0, 10) } }}</div>
    <input
      class="input1"
      :value="String(val)"
      @input="(e) => (val = e.target.value)"
    />
  </div>
</template>

  • 有时候正常工作: valhello, 输入框也显示 hello

image

  • 有时候不能: valhello, 但输入框为空 !!!!

image

  • 版本:
"vue": "^3.5.13"
"webpack": "^5.96.1"
"mp-webpack-plugin": "^1.6.6"

tesla-cat avatar Dec 14 '24 11:12 tesla-cat

  • 做了很多尝试 最后这么做才解决
<script setup>
import { onMounted, ref, watch } from 'vue'

defineProps(['ph'])
const val = defineModel()
const elm = ref(null)

watch(val, update)
onMounted(() => update(false))

function update(focus = true) {
  const e = elm.value
  if (focus) e.focus()
  e._oldValues = { focus: true }
  e.value = val.value || ''
}
</script>

<template>
  <input
    ref="elm"
    class="input1"
    :placeholder="ph"
    @input="(e) => (val = e.target.value)"
  />
</template>


tesla-cat avatar Dec 14 '24 13:12 tesla-cat

我在 demo26 补了下 case,没有复现。话说这里为什么要直接赋值给 value,而不是用 v-model ?

JuneAndGreen avatar Jun 09 '25 07:06 JuneAndGreen