zoukankan      html  css  js  c++  java
  • vue指令总结

    new  Vue({
             el: "#box",            // element(元素) 当前作用域
             data:{
        msg:"hello!"
     
               },
              methods:{
          changecontent(){
            this.msg='hi!'
          }
        }
            });

    一、v-model双向绑定数据 

    <input type="text" v-model="msg"/>   {{msg}}      // "hello!"  <!--取数据-->
     

    二、v-for循环

     
    复制代码
     1 <div id="box">
     2     <ul>
     3         <!--ng-repeat-->
     4         <li v-for="item in arr">
     5             <span>{{item.name}}</span>
     6             <span>{{item.age}}</span>
     7         </li>
     8     </ul>
     9 </div>
    10 <script type="text/javascript">
    11     new Vue({
    12         el:'#box',
    13         data(){
    14             return{
    15 //                arr:['module','views','controlle','aaaaa']
    16                 arr:[
    17                     {"name":"xiaohong1","age":12},
    18                     {"name":"xiaohong2","age":12},
    19                     {"name":"xiaohong3","age":12},
    20                     {"name":"xiaohong4","age":12}
    21                 ]
    22             }
    23         }
    24     })
    25 </script>
    复制代码

    三、v-show 显示与隐藏

    传递的值为布尔值  true  false  默认为false
    复制代码
    <div id="box">
        <div style=" 100px;height: 100px;background: black;display: none" v-show="show"></div>
    </div>
    </body>
    <script>
        new Vue({
            el: "#box",
            data(){
                return {
                    show: true
                }
            }
        })
    </script>
    复制代码

    四、v-if显示与隐藏  

    与v-show的区别:v-if的隐藏相当于给代码加注释,也就是删除了DOM元素;v-show相当于在操作display属性,隐藏后DOM元素仍然存在,占用内存。

    复制代码
    <div id="box">
        <div style=" 100px;height: 100px;background: black;" v-if="show"></div>
    </div>
    
    <script>
        new Vue({
            el: "#box",
            data(){
                return {
                    show: true
                }
            }
        })
    </script>
    复制代码

    五、v-else与v-else-if(必须与v-if一起使用)

    复制代码
    <div id="box">
        <div style=" 100px;height: 100px;background: black;" v-if="show"></div>
        <div style=" 300px;height: 300px;background: blue" v-else=""></div>
    </div>
    
    <script>
        new Vue({
            el: "#box",
            data(){
                return {
                    show: true
                }
            }
        })
    </script>
    复制代码

    v-else-if

    复制代码
    <div id="box">
        <div style=" 100px;height: 100px;background: black;" v-if="show"></div>
        <div style=" 100px;height: 100px;background: aqua;" v-else-if=""></div>
        <div style=" 300px;height: 300px;background: blue" v-else=""></div>
    </div>
    
    
    
    <script>
        new Vue({
            el: "#box",
            data(){
                return {
                    show: true
                }
            }
        })
    </script>
    复制代码

    六、v-bind(简写:)

    v-bind:class   三种绑定方法  1、对象型  '{red:isred}'  2、三目型   'isred?"red":"blue"'   3、数组型  '[{red:"isred"},{blue:"isblue"}]'
    复制代码
    <div id="box">
        <input type="text" v-bind:value="msg">
        <a :href="link">点击</a>
    </div>
    <script>
        new Vue({
            el: "#box",
            data(){
                return {
                    msg: "12222",
                    link:"1、v-model.html"
                }
            }
        })
    </script>
    复制代码

    七、v-on 事件

    复制代码
    <div id="box">
        <!-- v-on -->
        <button v-on:click="say">按钮</button>
        <!--<button @click="say">按钮</button>-->
    </div>
    
    <script>
        new Vue({
            el: "#box",
            data(){
                return {}
            },
            methods: {
                say() {
                    alert(111);
                }
            }
        })
    </script>
    复制代码

    八、v-text读取文本

    不能读取html标签

    复制代码
     1 <div id="box">
     2     <div v-text="msg"></div>
     3 </div>
     4 
     5 <script>
     6     new Vue({
     7         el: "#box",
     8         data(){
     9             return {
    10                 msg:"11111"
    11             }
    12         },
    13         methods: {
    14             say() {
    15                 alert(111);
    16             }
    17         }
    18     })
    19 </script>
    复制代码

    九、v-html  

    读取html标签

    复制代码
    <div id="box">
        <div v-html="msg"></div>
    </div>
    
    <script>
        new Vue({
            el: "#box",
            data(){
                return {
                    msg:"<h1>121212</h1>"
                }
            },
            methods: {
                say() {
                }
            }
        })
    </script>
    复制代码

    十、v-class (类名)与v-style

    复制代码
     1 <style>
     2         .red {
     3 
     4             background: red;
     5         }
     6 
     7         .blue {
     8              100px;
     9             height: 100px;
    10             background: blue;
    11         }
    12 
    13     </style>
    14 
    15 
    16 <div id="box">
    17     <div style=" 100px;height: 100px;" v-bind:class='{red:isred}'></div>
    18     <!--<div style=" 100px;height: 100px;" v-bind:class='isred?"red":"blue"'></div>-->    <!--三元运算符方式-->
    19     <!--<div style=" 100px;height: 100px;" v-bind:class='[{red:"isred"}]'></div>-->
    20 
    21 </div>
    22 
    23 
    24 <script>
    25     new Vue({
    26         el: "#box",
    27         data(){
    28             return {
    29                 isred:false
    30             }
    31         }
    32     })
    33 </script>
    复制代码

     十一、v-once  

    就是  加载一次  如果用到事件中就是事件只执行一次(@click.once="show")

    复制代码
    <div id="box">
        <div v-once>{{msg}}</div>
    </div>
    
    
    <script type="text/javascript">
        new Vue({
            el:"#box",
            data(){
                return{
                    msg:"qwdqwdqwd"
                }
            }
        })
    </script>
    复制代码

    十二、v-cloak防闪烁

    复制代码
    <div id="box">
        <div v-cloak="">欢迎--{{msg}}</div>
    </div>
    
    
    <script>
        new Vue({
            el:"#box",
            data(){
                return{
                    msg:"111111"
                }
            }
        })
    </script>
    复制代码

    十三、v-pre  

    把标签内部的元素原位输出

    复制代码
    <div id="box">
        <div v-pre>欢迎--{{msg}}</div>
    </div>
    
    
    
    <script>
        new Vue({
            el:"#box",
            data(){
                return{
                    msg:"111111"
                }
            }
        })
    </script>
    复制代码
  • 相关阅读:
    30-Transformation(HDU4578)-区间线段树(复杂)
    87-区间线段树(板子)--那个苑区的人最瘦
    86-区间线段树-模板
    1-2018-3-2小球碰撞
    85-取石子-威佐夫博弈
    83-取石子-尼姆博弈
    82-珠子染色-置换群
    2018.3.29 设计模式之单例模式详解
    2018.3.27 Mac 配置Tomcat
    2018.3.26 Linux下学习命令
  • 原文地址:https://www.cnblogs.com/phoebeyue/p/9215538.html
Copyright © 2011-2022 走看看