• Vue.js—Difference between v-model and v-bind


    Vue.js—Difference between v-model and v-bind

    I'm learning Vue with an online course and the instructor gave me an exercise to make an input text with a default value. I completed it using v-model but, the instructor chose v-bind:value and I don't understand why.

    Can someone give me a simple explanation about the difference between these two and when it's better use each one?

    回答1

    From here - Remember:

    <input v-model="something">
    

    is essentially the same as:

    <input
       v-bind:value="something"
       v-on:input="something = $event.target.value"
    >
    

    or (shorthand syntax):

    <input
       :value="something"
       @input="something = $event.target.value"
    >
    

    So v-model is a two-way binding for form inputs. It combines v-bind, which brings a js value into the markup, and v-on:input to update the js value.

    Use v-model when you can. Use v-bind/v-on when you must :-) I hope your answer was accepted.

    v-model works with all the basic HTML input types (text, textarea, number, radio, checkbox, select). You can use v-model with input type=date if your model stores dates as ISO strings (yyyy-mm-dd). If you want to use date objects in your model (a good idea as soon as you're going to manipulate or format them), do this.

    v-model has some extra smarts that it's good to be aware of. If you're using an IME ( lots of mobile keyboards, or Chinese/Japanese/Korean ), v-model will not update until a word is complete (a space is entered or the user leaves the field). v-input will fire much more frequently.

    v-model also has modifiers .lazy, .trim, .number, covered in the doc.

    回答2

    In simple words v-model is for two way bindings means: if you change input value, the bound data will be changed and vice versa.

    but v-bind:value is called one way binding that means: you can change input value by changing bound data but you can't change bound data by changing input value through the element.

    check out this simple example: https://jsfiddle.net/gs0kphvc/

  • 相关阅读:
    [Linux]yum开启rpm包缓存
    [Linux]centOS7-1-1503-x86_64下安装VM-TOOLS
    [Linux]centOS7下RPM安装Perl
    vue 之 pageage.json 和 webpack.config.js
    node 之 apache
    node 之 express
    node 之 基础知识
    npm nvm nrm的关系
    echarts 学习笔记
    git 操作学习
  • 原文地址:https://www.cnblogs.com/chucklu/p/14200234.html
Copyright © 2020-2023  润新知