Vue学习(三):Vue常用特性

在这里插入图片描述

表单操作

  • 元素操作
<!DOCTYPE html>
<head>
    <style></style>
    <title>选项卡</title>
</head>
<body>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <div id="hello">
        <form>
            <p>
                <span>姓名</span><input type="text"  v-model="name">
            </p> 
            <p>
                <input type="radio" name="sex" value="male" v-model="gender">Male
            </p>
            <p>
                <input type="radio" name="sex" value="female" v-model="gender">Female
            </p>
            <p>
                <span>爱好</span>
                <input type="checkbox" v-model="hobby" value="baseketball"><span>篮球</span>
                <input type="checkbox" v-model="hobby" value="music"><span>音乐</span>
                <input type="checkbox" v-model="hobby" value="football"><span>足球</span>
            </p>
            <p>
                <select>
                    <option>请选择技能...</option>
                    <option value ="css">css</option>
                    <option value ="javascript">javascript</option> 
                    <option value="html">html</option>
                  </select>
            </p>
            <textarea v-model="desc"></textarea>
        </form>
    </div>
    <script>
        var vm = new Vue({
            el:'#hello',
            data:{
                name:'nianyuyu',
                gender:'female',
                hobby:['baseketball','music','football'],
                desc:'hello world'
            }
        });
    </script>
</body>
</html> 
  • 表单域修饰符
<!DOCTYPE html>
<head>
    <style></style>
    <title>选项卡</title>
</head>
<body>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <div id="hello">
            <p>
                <span>输入数字:</span><input type="text"  v-model.number="age">
            </p>
            <button @click="handleClick1">计数</button>
            <p>
                <span>输入文本:</span><input type="text"  v-model.trim="text1">
            </p>
            
            <button @click="handleClick2">计算字符数</button>
            <p>
                <span>输入文本:</span><input type="text"  v-model="text2">
            </p>
            <button @click="handleClick3">计算字符数</button>

            <p>
                <span>输入文本:</span><input type="text"  v-model="text3">
            </p>
            <div>{{text3}}</div>
            <p>
                <span>输入文本:</span><input type="text"  v-model.lazy="text4">
            </p>
            <div>{{text4}}</div>
      
    </div>
    <script>
       
        var vm = new Vue({
            el:'#hello',
            data:{
                age:22,
                text1:'hello world',
                text2:'hello wo  rld',
                text3:'apple',
                text4:'apple'
            },
            methods:{
                handleClick1:function(){
                    this.age = this.age+13;
                },
                handleClick2:function(){
                    console.log(this.text1.length);
                },
                handleClick3:function(){
                    console.log(this.text2.length);
                },
                handleClick4:function(){
                    console.log(this.text3);
                }
            }
        });
    </script>
</body>
</html> 

 

自定义指令

  • 不带参数的全局自定义指令
<!DOCTYPE html>
<head>
    <style></style>
    <title>选项卡</title>
</head>
<body>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <div id="hello">
        <p>
            <!--
                 v-focus 使用自定义指令
            -->
            <input type="text"  v-model="text" v-focus>
        </p>  
    </div>
    <script>
        // 不带参数的自定义指令的语法规则
        Vue.directive('focus',{
            inserted:function(el){
                el.focus();
           }
        })
        var vm = new Vue({
            el:'#hello',
            data:{
                text:''
            },  
        });
    </script>
</body>
</html> 
  • 带参数的全局自定义指令
<!DOCTYPE html>
<head>
    <style></style>
    <title>选项卡</title>
</head>
<body>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <div id="hello">
        <p>
            <!--
                 v-focus 使用自定义指令
            -->
            <input type="text"  v-model="text" v-color='{color:"orange"}'>
            <input type="text"  v-model="text" v-color='msg'>
        </p>  
    </div>
    <script>
        // 带参数的自定义指令的语法规则
        Vue.directive('color',{
            bind:function(el,binding){
                console.log(binding);  // object
                el.style.backgroundColor = binding.value.color
           }
        })
        var vm = new Vue({
            el:'#hello',
            data:{
                text:'hello world',
                msg:{
                    color:'red'
                }
            },
           
        });
    </script>
</body>
</html> 
  • 局部自定义指令
<!DOCTYPE html>
<head>
    <style></style>
    <title>选项卡</title>
</head>
<body>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <div id="hello">
        <p>
            <!--
                 v-focus 使用自定义指令
            -->
            <input type="text"  v-model="text" v-focus>
        </p>  
    </div>
    <script> 
        var vm = new Vue({
            el:'#hello',
            data:{
                text:'hello world',
                msg:{
                    color:'red'
                }
            },
            // 局部自定义属性
            directives:{
                focus:{
                    inserted:function(el){
                        el.focus()
                    }
                }
            } 
        });
    </script>
</body>
</html> 

 

计算属性

  • 为什么需要计算指令
    表达式的计算逻辑可能会比较复杂,使用计算指令可以使模板内容更加简洁。

  • 使用计算指令

<!DOCTYPE html>
<head>
    <style></style>
    <title>选项卡</title>
</head>
<body>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <div id="hello">
        <p>{{msg}}</p>  
        <!--
         使用计算指令
        -->
        <p>{{reverseFunction}}</p>  
    </div>
    <script> 
        var vm = new Vue({
            el:'#hello',
            data:{
                msg:'helloWorld',
            },
            computed:{
                reverseFunction:function(){
                    return this.msg.split('').reverse().join('');
                }
            }
        });
    </script>
</body>
</html> 
  • 计算指令与方法的区别
    计算属性:计算属性是基于它们的依赖进行缓存的
    方法:方法不存在缓存
<!DOCTYPE html>
<head>
    <style></style>
    <title>选项卡</title>
</head>
<body>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <div id="hello">
        <!--
            注意两者的调用区别
        -->
        <p>{{reverseFunction}}</p>  
        <p>{{reverseFunction}}</p>  
        <p>{{reverseMsg()}}</p>  
        <p>{{reverseMsg()}}</p> 
    </div>
    <script> 
        var vm = new Vue({
            el:'#hello',
            data:{
                msg:'helloWorld',
            },
            methods:{
                reverseMsg:function(){
                    console.log("方法");   // 方法不存在缓存
                    return this.msg.split('').reverse().join('');
                }
            },
            computed:{
                reverseFunction:function(){
                    console.log("计算属性")  // 计算属性是基于他们的依赖进行缓存的
                    return this.msg.split('').reverse().join('');
                }
            }
        });
    </script>
</body>
</html> 

 

过滤器

  • 什么是过滤器
    在这里插入图片描述

  • 过滤器的使用场景
    格式化数据,比如将字符串格式化为首字母大写,将日期格式化为指定的格式等

  • 带参数的过滤器

<!DOCTYPE html>
<head>
    <style></style>
    <title>监听器</title>
</head>
<body>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <div id="hello">
        <!--
            过滤器的使用,|分隔,可以使用多个
        -->
        <div>{{msg | upper | lower}}</div>
    </div>
    <script> 
        Vue.filter('upper',function(val){
            return val.charAt(0).toUpperCase()+val.slice(1);
        })
        Vue.filter('lower',function(val){
            return val.charAt(0).toLowerCase()+val.slice(1);
        })
        var vm = new Vue({
            el:'#hello',
            data:{
                userName:'',
                msg:'hello',
            },
           
        });
    </script>
</body>
</html> 
  • 带参数的过滤器
<!DOCTYPE html>
<head>
    <style></style>
    <title>监听器</title>
</head>
<body>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <div id="hello">
        <!--
            监听器的使用,|分隔,可以使用多个
        -->
        <div>{{msg | upper('abc') | lower}}</div>
    </div>
    <script> 
        Vue.filter('upper',function(val,args){
            console.log(args);  //abc
            return val.charAt(0).toUpperCase()+val.slice(1);
        })
        Vue.filter('lower',function(val){
            return val.charAt(0).toLowerCase()+val.slice(1);
        })
        var vm = new Vue({
            el:'#hello',
            data:{
                userName:'',
                msg:'hello',
            },
           
        });
    </script>
</body>
</html> 

 

侦听器

  • 什么是侦听器
    在这里插入图片描述
  • 侦听器的应用场景
    数据变化时执行异步或开销较大的操作
<!DOCTYPE html>
<head>
    <style></style>
    <title>项目2:监听器校验用户名</title>
</head>
<body>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <div id="hello">
        <input type="text" v-model="userName">
        {{msg}}
    </div>
    <script> 
        var vm = new Vue({
            el:'#hello',
            data:{
                userName:'',
                msg:'',
            },
            methods:{
                checkName:function(userName){
                    var thisValue = this;
                    // 模拟异步调用
                    setTimeout(function(){
                    if(userName == 'admin') {
                        thisValue.msg = "该用户名已经注册" 
                    }else {
                        thisValue.msg = "该用户名可以使用"
                    }
                    },2000);
                }
            },
            watch:{
                userName:function(val){
                    this.checkName(val);
                    this.msg = "正在校验"
                },
            }
           
        });
    </script>
</body>
</html> 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_21439711

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值