Vue过滤器和组件

 过滤器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="js/vue.js" type="text/javascript"></script>
</head>
<body>
    <div id="app">
        <input type="text" v-model="msg">
        {{msg |upper}}
        {{msg |lower}}
        {{msg |test}}
        {{data |formate("yyyy-MM-dd hh:mm:ss")}}
    </div>
    <script type="text/javascript">
        //全局过滤器
        // 调用  {{ msg | 过滤器名字() }} upper过滤器名字,value过滤的数据
        Vue.filter('upper',(value) => {
            //把输入的数据首字母转化为大写
            return value.charAt(0).toUpperCase()+value.slice(1);
        })
        Vue.filter('lower',(value) => {
            //把输入的数据首字母转化为小写
            return value.charAt(0).toLowerCase()+value.slice(1);
        })
        //转换时间格式
        Vue.filter('formate',function(value,arg1){
            if(arg1=='yyyy-MM-dd hh:mm:ss'){
                var t="";
                //getFullYear年 getMonth月 getDate日 getHours时 getMinutes分 getSeconds秒
                t+=value.getFullYear()+"-"+value.getMonth()+"-"+value.getDate()+" "+value.getHours()+":"+value.getMinutes()+":"+value.getSeconds();
                return t;
            }
            return value;
        })
        new Vue({
            el:"#app",
            data:{
                msg:"",
                data:new Date()
            },
            //局部过滤器
            filters: {
                test:function(val){
                    //把输入的数据首字母转化为大写 test过滤器名字,val过滤的数据
                    return val.charAt(0).toUpperCase()+val.slice(1);
                }
            }
        })
    </script>
</body>
</html>

组件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="js/vue.js" type="text/javascript"></script>
</head>
<body>
    <div id="app">
        <!-- 三个互不打扰,分别计算次数 -->
       <button-conianer></button-conianer>
       <button-conianer></button-conianer>
       <button-conianer></button-conianer>

       <!-- 在这里驼峰命名方式也需要改为短横线 -->
       <hello-word></hello-word>
       <!-- 局部组件 -->
        <hello-dog></hello-dog>
        <hello-tom></hello-tom>
        <hello-jerry></hello-jerry>
    </div>
    <script type="text/javascript">
        //组件命名方式 短横线方式
        Vue.component('button-conianer',{
            //data必须是一个函数
            data:function(){
                return{
                    count:0
                }
            },
            //组件模板内容必须是单个根元素
            // template:"<button v-on:click='count++'>点击了{{count}}次</button>"
            // template:"<div><button v-on:click='count++'>点击了{{count}}次</button><button>abc</button></div>"
            //增强代码观赏性 可以在开头和结尾加上 ` 就能够换行了
            template:
            `
            <div>
                <button v-on:click='count++'>点击了{{count}}次</button>
                <button>abc</button>
            </div>
            `
        })
        //组件命名方式 驼峰方式
        Vue.component('HelloWord',{
            //data必须是一个函数
            data:function(){
                return{
                    msg:"helloword"
                }
            },
            //组件模板内容必须是单个根元素
            // template:"<button v-on:click='count++'>点击了{{count}}次</button>"
            // template:"<div><button v-on:click='count++'>点击了{{count}}次</button><button>abc</button></div>"
            //增强代码观赏性 可以在开头和结尾加上 ` 就能够换行了
            template:
            `
            <div>
                {{msg}}
            </div>
            `
        });
        //局部组件需要var来定义
        var hellodog={
            data:function(){
                return{
                msg:"hello-dog"
            }
        },
        template:
        `
        <div>
        {{msg}}    
        </div>
        `
        };
        var hellotom={
            data:function(){
                return{
                msg:"hello-tom"
            }
        },
        template:
        `
        <div>
        {{msg}}    
        </div>
        `
        };
        var hellojerry={
            data:function(){
                return{
                msg:"hello-jerry"
            }
        },
        template:
        `
        <div>
        {{msg}}    
        </div>
        `
        };
        new Vue({
            el:"#app",
            data:{
              
            },
            //局部组件
            components:{
                "hello-dog":hellodog,
                "hello-tom":hellotom,
                "hello-jerry":hellojerry
            }
        })
    </script>
</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值