Vue 双向绑定

前端开发 作者: 2024-08-20 09:30:01
devtools工具 使用devtools工具可以让你更加方便的查看到Vue实例中数据的变化。 在chorme商店搜索安装即可。 双向绑定 v-model 双向绑定一般都是与input家族进行绑定。

devtools工具

双向绑定

<body>

<div id="app">
    <input type="text" v-model="v">
</div>

<script src="./vue.js"></script>
<script>
    const app = new Vue({
        el:"#app",data:{
            v:null,},})
</script>
</body>
<body>

<div id="app">
    <input type="text" v-model="v">
</div>

<script src="./vue.js"></script>
<script>
    const app = new Vue({
        el: "#app",data: {
            v: null,watch: {
            v(newValue,oldValue) {
                console.log(`${oldValue}=>${newValue}`);
            }
        }
    })
</script>
</body>

表单操作

<body>

<div id="app">
    <form action="">
        <div>
            <label for="username">
                <p>用户名</p>
                <input type="text" id="username" v-model="username">
            </label>
        </div>
        <p></p>
        <div>
            <label for="concise">
                <p>个人简介</p>
                <textarea name="" id="concise" rows="10" cols="30" v-model="concise"></textarea>
            </label>
        </div>
    </form>
</div>

<script src="./vue.js"></script>
<script>
    const app = new Vue({
        el: "#app",data: {
            username: "",concise: ""
        },})
</script>
</body>
<body>

<div id="app">
    <form action="">
        <p>我同意协议<input type="checkbox" v-model="status"></p>
        <button :disabled="!status">下一步</button>
    </form>
</div>

<script src='./vue.js'></script>
<script>
    "use strict";
    const app = new Vue({
        el: "#app",data: {
            status: false,});
</script>
</body>
<body>

<div id="app">
    <form action="">
        <p v-for="item in hobby">
            {{item.name}}<input type="checkbox" v-model="choice" :value="item.id">
        </p>
    </form>
</div>

<script src='./vue.js'></script>
<script>
    "use strict";
    const app = new Vue({
        el: "#app",data: {
            choice: [],hobby: [
                {"id": 1,"name": "篮球"},{"id": 2,"name": "足球"},]
        },});
</script>
</body>
<body>

<div id="app">
    <form action="">
        <!-- 使用v-bind,将会是int类型,否则是string -->
        男<input type="radio" v-model="gender" :value=1>
        女<input type="radio" v-model="gender" :value=2>
    </form>
</div>

<script src='./vue.js'></script>
<script>
    "use strict";
    const app = new Vue({
        el: "#app",data: {
            gender: 1,});
</script>
</body>
<body>

<div id="app">
    <form action="">
        <select name="" v-model="choice" multiple>
            <option :value="item.id" v-for="item in city">{{item.name}}</option>
        </select>
    </form>
</div>

<script src='./vue.js'></script>
<script>
    "use strict";
    const app = new Vue({
        el: "#app",city: [
                {"id": 1,"name": "北京"},"name": "上海"},{"id": 3,"name": "广州"},],});
</script>
</body>

修饰符

<body>

<div id="app">
    <form action="">
        <input type="text" v-model.lazy="userInput">
    </form>
</div>

<script src='./vue.js'></script>
<script>
    "use strict";
    const app = new Vue({
        el: "#app",data: {
            "userInput": "默认值",});
</script>
</body>
<body>

<div id="app">
    <form action="">
        <input type="text" v-model.number="userInput">
    </form>
</div>

<script src='./vue.js'></script>
<script>
    "use strict";
    const app = new Vue({
        el: "#app",});
</script>
</body>
<body>

<div id="app">
    <form action="">
        <input type="text" v-model.trim="userInput">
    </form>
</div>

<script src='./vue.js'></script>
<script>
    "use strict";
    const app = new Vue({
        el: "#app",});
</script>
</body>

实例练习

<body>
<div id="app">

    <div class="container">
        <div class="row">
            <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6 col-md-offset-2">
                <form action="" method="POST" role="form">
                    <p></p>
                    <legend class="text-center">欢迎注册</legend>
                    <div class="form-group">
                        <label for="username">用户名</label>
                        <input type="text" class="form-control" id="username" v-model="register.username">
                    </div>
                    <div class="form-group">
                        <label for="age">出生日期</label>
                        <input type="text" class="form-control" id="age" v-model.number="register.age">
                    </div>
                    <div>
                        <label class="radion-inline" for="male">男
                            <input type="radio" id="male" value="male" checked="checked" v-model="register.gender">
                        </label>
                        <label class="radion-inline" for="female">女
                            <input type="radio" id="male" value="female" v-model="register.gender">
                        </label>
                    </div>
                    <p></p>
                    <div class="form-group">
                        <label for="hobby">兴趣爱好</label>
                        <select id="hobby" class="form-control" required="required" multiple v-model="register.hobby">

                            <option value="1">篮球</option>
                            <option value="2">足球</option>
                            <option value="3">其他</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="introduction">个人简介</label>
                        <textarea id="introduction" cols="30" rows="10" class="form-control"
                                  v-model="register.introduction"></textarea>
                    </div>

                    <button type="注册" class="btn btn-primary" @click.prevent="post">Submit</button>
                </form>
            </div>
        </div>
    </div>
</div>
<script src='./vue.js'></script>
<script>
    "use strict";
    const app = new Vue({
        el: "#app",data: {
            register: {
                username: null,gender: null,age: null,hobby: [],introduction: "",methods: {
            post() {
                console.log(this.register);
            }
        }
    });
</script>
</body>
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_65442.html
Vue 双向绑定