VUE——常用指令

v-bind 

作用:为HTML标签绑定属性值。

<a v-bind:href=""></a>
//省略版
<a :href=""></a>

v-model

作用:在表单元素上创建双向数据绑定。

<input type="text" v-model="">

案例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>常用指令</title>
    <script src="js/vue.js"></script>
</head>
<body>
    <div id="vv">
        <a v-bind:href="url">链接1</a>
        <a :href="url">链接2</a>
        <input type="text" v-model="url">{{url}}
    </div>
</body>
<script>
    new Vue({
        el: "#vv",
        data: {
            url: "https://www.baidu.com"
        }   
    })
</script>
</html>

v-on

作用:为HTML标签绑定事件

<input type="button" value="" v-on:event="func()">
<input type="button" value="" @event="func()">
//event=click/focus/blur(点击/聚焦/离散)

new Vue({
    el: "",
    data: {
    },
    methods: {
        func:function(){
        }
    },
})

案例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>常用指令</title>
    <script src="js/vue.js"></script>
</head>
<body>
    <div id="vv">
        <input type="button" value="点击1" v-on:click="h()">
        <input type="button" value="点击2" @click="h()">
    </div>
</body>
<script>
    new Vue({
        el: "#vv",
        data: {
        },
        methods: {
            h:function(){
                alert("ok");
            }
        },
    })
</script>
</html>

v-if、v-else-if、v-else

作用:条件性的渲染某元素,判定为true时渲染,否则不渲染。

<span v-if=""></span>
<span v-if=""></span>
<span v-if=""></span>

v-show

作用:根据条件展示某元素,区别在于切换的是display属性的值。

<span v-show=""></span>

案例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>常用指令</title>
    <script src="js/vue.js"></script>
</head>
<body>
    <div id="vv">
        v-if、v-else-if、v-else:<br>
        年龄:<input type="text" v-model="age">
        <br>经判定为:
        <span v-if="age < 35">年轻人</span>
        <span v-if="age >= 35 && age <60">中年人</span>
        <span v-if="age >= 60">老年人</span>
        <br><br>
        v-show:<br>
        年龄:<input type="text" v-model="age">
        <br>经判定为:
        <span v-show="age < 35">年轻人</span>
        <span v-show="age >= 35 && age <60">中年人</span>
        <span v-show="age >= 60">老年人</span>

    </div>
</body>
<script>
    new Vue({
        el: "#vv",
        data: {
            age: 22
        },
        methods: {
            }
        },

    })
</script>
</html>

v-for

作用:列表渲染,遍历容器的元素或者对象的属性。

//方法一
<div v-for="(item, index) in items"></div>
//方法二
<div v-for="item in items"></div>

案例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>常用指令</title>
    <script src="js/vue.js"></script>
</head>
<body>
    <div id="vv">
        <div v-for="(addr, index) in addrs">
            {{index}}:{{addr}}
        </div>
        <br>-----------------------------<br>
        <div v-for="(addr, index) in addrs">
            {{index+1}}:{{addr}}
        </div>
        <br>-----------------------------<br>
        <div v-for="addr in addrs">
            {{addr}}
        </div>
        <div v-for="(item, index) in items"></div>
    </div>

</body>
<script>
    new Vue({
        el: "#vv",
        data: {
            addrs: ["北京" ,"福建","广东"]
        },

    })
</script>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值