search

Home  >  Q&A  >  body text

javascript - Can input not be used at the same time: value and v-model?

<template>
    <p id="login">
        <p>
            用户: <input type="text" v-model="username" :value='info.name'>
            密码: <input type ="text" v-model="password" :value='info.psd'>
        </p>
    </p>
</template>
<script>
    export default{
        name:'login',
        data(){
            return{
                username:'',
                password:'',
                info:{
                    name:'123',
                    psd:'123',
                },
            }
        },
    }
</script>

I want the input to initially display the value in info, and then I can use the value of v-model, but the effect cannot be achieved. The value of info is not displayed. Is it wrong to write this way? How should I write to achieve my needs?

phpcn_u1582phpcn_u15822745 days ago851

reply all(5)I'll reply

  • 天蓬老师

    天蓬老师2017-07-05 10:39:57

                return{
                    username:'123',
                    password:'123',
                }

    It’s two-way anyway, why bother adding more.

    reply
    0
  • 怪我咯

    怪我咯2017-07-05 10:39:57

    It is recommended to remove v-bind:value and write info.name and info.psd directly to v-model. The code is as follows:

    <template>
        <p id="login">
            <p>
                用户: <input type="text" v-model="username">
                密码: <input type ="text" v-model="password">
            </p>
        </p>
    </template>
    <script>
        export default{
            name:'login',
            data(){
                return{
                    username:'123',
                    password:'123',
                    info:{
                        name:'123',
                        psd:'123',
                    },
                }
            },
        }
    </script>

    When the value of the input changes, the username and password also change

    reply
    0
  • 巴扎黑

    巴扎黑2017-07-05 10:39:57

    v-model is the syntax sugar for v-bind:input and v-bind:value.

    reply
    0
  • 代言

    代言2017-07-05 10:39:57

    Solved, just use an input and a p

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-07-05 10:39:57

    https://jsfiddle.net/stardew/...

    reply
    0
  • Cancelreply