longjiahui
V2EX  ›  Vue.js

请问 vue3 input 绑定一个常量 value 后咋还能输入咧

  •  
  •   longjiahui ·
    longjiahui · Oct 4, 2022 · 3364 views
    This topic created in 1512 days ago, the information mentioned may be changed or developed.

    请问 vue3 中 input 怎么做控制输入内容的需求会比较好

    <script>
    export default {
    }
    </script>
    
    <template>
    	<input :value="1234" placeholder="edit me" />
    </template>
    

    类似这样,input 仍然是可以输入的 😢

    Supplement 1  ·  Oct 4, 2022
    <script setup>
      import { ref, watch } from 'vue'
      let data = ref('test')
      
      function handleInput(e){
        data.value = 'hello'
        
        // 少了这句input 就能够一直输入东西
        // 那么input元素的:value 并不能确保binding的value的内容了。而是需要自己手动操作dom元素?
        e.target.value = 'hello'
      }
    </script>
    
    <template>
    	<input :value="data" @input="handleInput($event)" placeholder="edit me" />
    </template>
    

    demo 👈

    Supplement 2  ·  Oct 4, 2022
    我暂时的解决方法是用 组件改了一下。
    ```
    <template>
    <input type="text" :value="value" @input="handleInput">
    </template>

    <script setup>
    let props = defineProps({
    value: String,
    })

    let emit = defineEmits(['update:value'])

    function handleInput(e){
    emit('update:value', e.target.value)
    e.target.value = props.value
    }
    </script>
    ```

    改之前好像是和 react 的 input 比较像
    19 replies  •  2022-10-05 12:08:03 +08:00
    cyrbuzz
    cyrbuzz
    PRO
    Oct 4, 2022
    1
    创建一个变量,:value 换成 v-model ,或者新建一个变量,注册 input 事件。
    biabia123456
    biabia123456
    Oct 4, 2022
    2
    做一个假的 input? 想让用户输入的时候 换成真的并聚焦
    longjiahui
    longjiahui
    OP
    Oct 4, 2022
    3
    @cyrbuzz 附言里加了说明、我总觉得我的操作不太科学。
    longjiahui
    longjiahui
    OP
    Oct 4, 2022
    4
    @biabia123456 现在想做的方法是 写个 input 组件模拟 vue2 时的特性来用,就是 binding 的 value 是啥,input 的输入框内容就显示啥。
    skies457
    skies457
    Oct 4, 2022
    5
    加上 @keydown="$event.preventDefault()"?
    longjiahui
    longjiahui
    OP
    Oct 4, 2022
    6
    @skies457 粗暴了点、input 事件不响应啦、响应 input 来修改内容还是比较方便的。
    cxe2v
    cxe2v
    Oct 4, 2022
    7
    似乎你可以加 readonly
    longjiahui
    longjiahui
    OP
    Oct 4, 2022
    8
    @cxe2v 试了呢、不响应 input
    lsry
    lsry
    Oct 5, 2022
    9
    data.value 是變量當然可以修改了。如果不想讓用戶輸入,給 input 加 disable ,不喜歡樣式的話,自己改一下吧。如果讓用戶輸入特定的内容,可以加校驗。或者你詳述下需求。
    renmu
    renmu
    Oct 5, 2022 via Android
    10
    没看懂你到底想做什么,v-model 不就能实现
    renmu
    renmu
    Oct 5, 2022 via Android
    11
    没看懂你到底想做什么,v-model 不能实现吗?为什么自己实现了一遍 v-model
    longjiahui
    longjiahui
    OP
    Oct 5, 2022
    14
    @WhateverYouLike yes !是可以的。我完全没想到 nexttick
    大概习惯了 vue2 的方式,vue3 这种 input 绑定 value 不能固定下来的感觉不太舒适。
    longjiahui
    longjiahui
    OP
    Oct 5, 2022
    15
    @renmu 给 input 绑定 value 无论如何 vue2 会显示 value 绑定的内容,但 vue3 就不一定了耶。可以参考内容和附言 1 ,2 的 demo
    Jaufey
    Jaufey
    Oct 5, 2022
    16
    @longjiahui Vue2 难道能 “固定” 吗?
    longjiahui
    longjiahui
    OP
    Oct 5, 2022
    17
    @WhateverYouLike 刚试了不能耶 😢我为什么觉得 2 可以。哈哈哈哈哈哈哈、非常有可能是因为我一直用的是封装过的 element-ui 的 input 。
    longjiahui
    longjiahui
    OP
    Oct 5, 2022
    18
    @renmu 是的,自己实现了一遍 v-model 💦
    cyrbuzz
    cyrbuzz
    PRO
    Oct 5, 2022
    19
    ```
    <script setup>
    import { ref } from 'vue'
    const data = ref('test');
    function handleInput(e){
    data.value = e.target.value
    }


    </script>

    <template>
    <input :value="data" @input="handleInput" placeholder="edit me" />
    </template>
    ```

    不用 v-model 这样不就好了嘛= =?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2768 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 04:27 · PVG 12:27 · LAX 20:27 · JFK 23:27
    ♥ Do have faith in what you're doing.