vue常用指令

v-model

双向数据绑定

一般用于表单元素

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>v-model</title>
    <script type="text/javascript" charset="utf-8" src="../js/vue.js"></script>
    <script type="text/javascript">
        window.onload=function () {
            new Vue({
                el:".itany",
                data:{
                    name:"",
                    age:18,
                    flag:true,
                    user:{id:"111",name:"aaaa"}
                }
            });
        }
    </script>
</head>
<body>
    <div class="itany">
        用户名:<input type="text" v-model="name"/>
        <br>
        {{name}}<br>
        {{age}}<br>
        {{flag}}<br>
        {{user}}<br>
        {{user.id}}<br>
        {{user.name}}<br>
    </div>
</body>
</html>

v-for

对数组或对象进行循环操作

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>v-for</title>
    <script type="text/javascript" charset="utf-8" src="../js/vue.js"></script>
    <script type="text/javascript">
        window.onload=function () {
            new Vue({
                el:"#itany",
                data:{
                    arr:[12,4,89,5,3,95,8],
                    user:{id:"1231",name:"saaaa",age:18},
                    arr2:[12,4,89,5,3,95,8,12],
                    users:[
                        {id:"1231",name:"saaaa",age:18},
                        {id:"1232",name:"sbbbb",age:19},
                        {id:"1233",name:"scccc",age:17}],
                }
            });
        }
    </script>
</head>
<body>
    <div id="itany">
        {{arr}}<br>
        <!--普通循环-->
        <ul>
            <li v-for="value in arr">{{value}}</li>
        </ul>
        <ul>
            <li v-for="value in user">{{value}}</li>
        </ul>
        <!--键值循环-->
        <ul>
            <li v-for="(value,index) in arr">{{value}}----------{{index+1}}</li>
        </ul>
        <ul>
            <li v-for="(value,index) in user">{{value}}----------{{index}}</li>
        </ul>
        <!--可以直接循环包含重复数据的集合-->
        <!--:key  可以通过指定:key属性绑定唯一key,
            当更新元素时可重用元素,提高效率-->
        <ul>
            <li v-for="(value,index) in arr2" :key="index">{{value}}</li>
        </ul>
        <!--循环对象数组,添加索引-->
        <ul>
            <li v-for="(user,index) in users">
                {{index+1}}:{{user.id}}----{{user.name}}-----{{user.age}}
            </li>
        </ul>
    </div>
</body>
</html>

v-on

用来绑定事件

用法:v-on:事件="函数"

<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>v-on</title>
    <script type="text/javascript" src="../js/vue.js" charset="utf-8"></script>
    <script type="text/javascript">
        window.onload=function () {
            new Vue({
                el:"#itany",
                data:{
                    arr:[12,34,12,22]
                },
                methods:{//定义方法
                    click:function () {
                        console.log("click-info");
                    },
                    add(){
                        /*this表示当前的vue实例*/
                        this.arr.push(666);
                        this.click();
                    }
                }
            });
        }
    </script>
</head>
<body>
    <div id="itany">
        <!--没有参数,可以不加小括号-->
        <button v-on:click="click()">CLICK</button>
        <button v-on:click="add()">ADD</button>
        <br>
        {{arr}}
        <br>
        <button v-on:mouseover="click">mouseover</button>
        <button v-on:dblclick="click">dblclick</button>
    </div>
</body>
</html>

 v-show/v-if

用来显示或隐藏元素

v-show是通过display实现

v-if是每次删除后再重新创建

<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>v-show/v-if</title>
    <script type="text/javascript" src="../js/vue.js" charset="utf-8"></script>
    <script type="text/javascript">
        window.onload=function () {
            new Vue({
                el:"#itany",
                data:{
                    flag:true,
                },
                methods:{
                    change(){
                        this.flag = false;
                    }
                }
            })
        }
    </script>
</head>
<body>
    <div id="itany">
        <button v-on:click="change()">HIDE</button>
        <hr>
        <button v-on:click="flag=false">HIDE</button>
        <hr>
        <button v-on:click="flag=!flag">HIDE/SHOW</button>
        <hr>
        <div style="width: 100px;height: 100px; background-color: lightpink" v-show="flag">
            AAAAAAAA
        </div>
    </div>
</body>
</html>

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值