Vue v-if v-for v-bind v-on

简介:

v-if

<div v-if="type === 'A'">
  A
</div>
<div v-else-if="type === 'B'">
  B
</div>
<div v-else-if="type === 'C'">
  C
</div>
<div v-else>
  Not A/B/C
</div>

v-for

<ul id="example-1">
  <li v-for="item in items">
    {{ item.message }}
  </li>
</ul>
var example1 = new Vue({
  el: '#example-1',
  data: {
    items: [
      {message: 'Foo' },
      {message: 'Bar' }
    ]
  }
})

v-bind

<!-- 绑定一个属性 -->
<img v-bind:src="imageSrc">
<!-- 缩写 -->
<img :src="imageSrc">
<!-- with inline string concatenation -->
<img :src="'/path/to/images/' + fileName">

v-on

<!-- 方法处理器 -->
<button v-on:click="doThis"></button>
<!-- 内联语句 -->
<button v-on:click="doThat('hello', $event)"></button>
<!-- 缩写 -->
<button @click="doThis"></button>
<!-- 停止冒泡 -->
<button @click.stop="doThis"></button>
<!-- 阻止默认行为 -->
<button @click.prevent="doThis"></button>
<!-- 阻止默认行为,没有表达式 -->
<form @submit.prevent></form>
<!--  串联修饰符 -->
<button @click.stop.prevent="doThis"></button>
<!-- 键修饰符,键别名 -->
<input @keyup.enter="onEnter">
<!-- 键修饰符,键代码 -->
<input @keyup.13="onEnter">

案例整合

<!DOCTYPE html>
<html lang="zh-cmn-Hans">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta content="telephone=no" name="format-detection" />
    <title>关注的人</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <link href="../css/qsc-user.css" rel="stylesheet" type="text/css">
    <link href="../css/ddd-dialog.css" rel="stylesheet" type="text/css">
    <style type="text/css">
        .cont {
            margin-top: 5em!important;
        }


        .tu img {
            display: block;
            width: 50%;
            margin: 10px auto;
        }

        body {
            background-color: #fff;
            font-family: "微软雅黑";
        }

        .aui-bar-nav {

            background: #00d8c9;
        }

        .color_organ{
            color: #ff901a;
        }

        [v-cloak] {
            display: none;
        }
    </style>
</head>

<body>
<header class="aui-bar aui-bar-nav">
    <a class="aui-pull-left aui-btn ">
        <span class="aui-iconfont aui-icon-left"></span>
    </a>
    <div class="aui-title">关注的人</div>
</header>
<div id="content" class="aui-content aui-margin-b-15" v-cloak>
    <div v-if="Items.length == 0">
        <div class="cont">
            <div class="tu">
                <img src="../image/nodata5.png">
            </div>
        </div>
    </div>
    <div v-else>
    <ul class="aui-list aui-media-list">
        <li class="aui-list-item aui-list-item-middle" v-for="Item in Items">
            <div class="aui-media-list-item-inner">
                <div class="aui-list-item-media" style="width: 3rem;">
                    <img v-bind:src="Item.Avatar" class="aui-img-round aui-list-img-sm">
                </div>
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-text">
                        <div class="aui-list-item-title aui-font-size-14" v-cloak>{{Item.Nickname}}
   
                        </div>
                    </div>
                    <div class="aui-list-item-text">
                        我直播,我快乐
                    </div>
                </div>
                <div class="aui-list-item-right aui-margin-r-15">
                    <input type="checkbox" class="aui-radio" checked v-on:click="follow(Item.RongCustomerId,$event)">
                </div>
            </div>
        </li>
    </ul>
    </div>
</div>


<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/config.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript" src="../script/vue.js"></script>
<script type="text/javascript" src="../script/jquery.min.js"></script>

<script>
    var user;
    apiready = function () {
        user = $api.getStorage('user');
        fix_status_bar();
        // 获取账户数据
        api.ajax({
            url : BASE_URL_ACTION + '/RongCloud/GetSubscriptionList',
            method : 'post',
            data : {
                values : {
                    rongCustomerId : user.person_id,
                    subscribe: 0,
                    pageIndex:0,
                    pageSize:20
                }
            }
        },function(json, err) {
            var header_vm = new Vue({
                el: '#content',
                data: json,
                methods:{
                    follow:function (RongCustomerId,e) {
                        var target = e.currentTarget;
                        if ($(target).is(":checked")) {// 关注
                            follow(RongCustomerId,user.person_id,1);
                        } else  { // 取消关注
                            follow(RongCustomerId,user.person_id,0);
                        }

                        // 发送页面刷新事件
                        api.sendEvent({
                            name: 'diamond_reload',
                            extra: {
                            }
                        });
                    }
                }
            })
        });

    }

    // 关注主播
    function follow(roomId,rongCustomerId,subscribe) { // subscribe 1关注 0取消关注
        api.ajax({
            url : BASE_URL_ACTION + '/RongCloud/ChatRoomSubscribe',
            method : 'post',
            data : {
                values : {
                    roomId:roomId, // 主播的融云id
                    rongCustomerId : rongCustomerId,
                    subscribe:subscribe
                }
            }
        }, function(json, err) {
        });
    }
</script>

</body>


</html>

本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6698551.html,如需转载请自行联系原作者


相关文章
|
1月前
|
JavaScript
在vue中,v-show 与 v-if 有什么区别?
在vue中,v-show 与 v-if 有什么区别?
22 4
N..
|
24天前
|
JavaScript 前端开发 开发者
Vue.js的v-for指令
Vue.js的v-for指令
N..
11 1
|
1月前
|
JSON JavaScript 算法
Vue之v-for(包含key内部原理讲解)
Vue之v-for(包含key内部原理讲解)
|
1月前
|
存储 监控 JavaScript
Vue之插值表达式,v-bind(单向绑定),v-model(双向绑定)
Vue之插值表达式,v-bind(单向绑定),v-model(双向绑定)
|
1月前
|
JavaScript 前端开发
vue.js中v-show,v-if的区别,以及使用场景
vue.js中v-show,v-if的区别,以及使用场景
21 0
|
1月前
|
JavaScript 开发者
描述 Vue 的模板语法,如 v-bind、v-on 等。
描述 Vue 的模板语法,如 v-bind、v-on 等。
15 2
|
2月前
|
JavaScript
Vue中的v-if和v-show在性能上有何区别?
Vue中的v-if和v-show在性能上有何区别?
21 1
|
JavaScript 测试技术 容器
Vue2+VueRouter2+webpack 构建项目
1). 安装Node环境和npm包管理工具 检测版本 node -v npm -v 图1.png 2). 安装vue-cli(vue脚手架) npm install -g vue-cli --registry=https://registry.
980 0
|
7天前
|
JavaScript 算法 Linux
【vue报错】error:0308010C:digital envelope routines::unsupported
【vue报错】error:0308010C:digital envelope routines::unsupported
34 3
|
2天前
|
JavaScript 测试技术
vue不同环境打包环境变量处理
vue不同环境打包环境变量处理
13 0