Vue2(初识vue)(四)

简介: Vue2(初识vue)

四,Vue过滤器


过滤器是指Vue.js支持在{{}}插值的尾部添加一个管道符“(|)”对数据进行过滤,经常用于格式化文本,比如字母的大写、货币的千位使用、逗号分隔等


示例


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vue2</title>
</head>
<body>
    <div id="app">
        {{ times | conversion("yyyy-MM-dd HH:mm:ss 星期w") | againChange}}
    </div>
</body>
<script src="../js/vue2.7.js"></script>
<script>
    // 创建一个Vue实例
    var app = new Vue({
        el:"#app", // 绑定一个元素
        data(){
            return {
                times: 1690944730436,
            }
        },
         // 2. 定义过滤器
        filters: {
            //3.定义一个处理函数,参数value为要处理数据,format为传入参数
            conversion: function (value, format) {
            //这个转换方法就不介绍了,看看就行,过滤器用法为主
            var date = new Date(value);
            function addZero(date) {
                if (date < 10) {
                return "0" + date;
                }
                return date;
            }
            let getTime = {
                yyyy: date.getFullYear(),
                yy: date.getFullYear() % 100,
                MM: addZero(date.getMonth() + 1),
                M: date.getMonth() + 1,
                dd: addZero(date.getDate()),
                d: date.getDate(),
                HH: addZero(date.getHours()),
                H: date.getHours(),
                hh: addZero(date.getHours() % 12),
                h: date.getHours() % 12,
                mm: addZero(date.getMinutes()),
                m: date.getMinutes(),
                ss: addZero(date.getSeconds()),
                s: date.getSeconds(),
                w: (function () {
                let a = ["日", "一", "二", "三", "四", "五", "六"];
                return a[date.getDay()];
                })(),
            };
            for (let i in getTime) {
                format = format.replace(i, getTime[i]);
            }
            return format;
            },
            //4.再定义一个过滤器,给数据前加上"时间为:"这几个字
            againChange: function (value) {
            return "时间为:" + value;
            },
        },
    })
</script>
</html>


953af9b2fbdda69cb76eb326fa44fa83_5ce80cb68dfa4208b29d502e89d624ff.png


五,vue中class样式的动态绑定


5.1,字符串写法

  • 使用场景
  • 样式的类型不确定
  • 手动触发样式改变
  • 注意

字符串使用的是vue实例data中的已有属性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vue2</title>
</head>
<style>
    .yangshi{
        font-size: 50px;
    }
    .yanse{
        color: aquamarine;
    }
</style>
<body>
    <div id="app">
        <div :class="myclass">你好vue</div>
    </div>
</body>
<script src="../js/vue2.7.js"></script>
<script>
    // 创建一个Vue实例
    var app = new Vue({
        el:"#app", // 绑定一个元素
        data(){
            return {
                myclass:'yangshi yanse',
            }
        },
    })
</script>
</html>


1d83abb99455914eaec3c58625ede84f_45d4658bd6a9493eb81972f8888a290c.png


5.1,对象写法

  • 使用场景
  • 样式个数、类名确定,通过Boolean动态展示与否
  • 对象写在内联样式
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vue2</title>
</head>
<style>
    .yangshi{
        font-size: 50px;
    }
    .yanse{
        color: aquamarine;
    }
</style>
<body>
    <div id="app">
        <div :class="{yangshi:isyangshi,yanse:isyanse}">你好</div>
    </div>
</body>
<script src="../js/vue2.7.js"></script>
<script>
    // 创建一个Vue实例
    var app = new Vue({
        el:"#app", // 绑定一个元素
        data(){
            return {
                isyangshi:true,
                isyanse:true,
            }
        },
    })
</script>
</html>

ceab6c50667cede7f3c224759fb0278f_722a2f43e8434e0abf62f439f93db2cc.png


5.1,数组写法

  • 使用场景
  • 需要绑定的样式个数不确定,类名也不确定
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vue2</title>
</head>
<style>
    .yangshi{
        font-size: 50px;
    }
    .yanse{
        color: aquamarine;
    }
</style>
<body>
    <div id="app">
        <div :class="myclassObject">你好</div>
    </div>
</body>
<script src="../js/vue2.7.js"></script>
<script>
    // 创建一个Vue实例
    var app = new Vue({
        el:"#app", // 绑定一个元素
        data(){
            return {
                myclassObject:['yangshi','yanse']
            }
        },
    })
</script>
</html>


4f3055accdb255b5bfd5bfb4758a3ed9_970a3ccc2db04f8e87b6a1b1d67a2e6a.png


六,完整代码


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .yangshi{
        font-size: 50px;
    }
    .yanse{
        color: aquamarine;
    }
</style>
<body>
    <div id="app">
        {{ times | conversion("yyyy-MM-dd HH:mm:ss 星期w") | againChange}}
        <div :class="myclass">你好</div>
        <div :class="{yangshi:isyangshi,yanse:isyanse}">你好</div>
        <div :class="myclassObject">你好</div>
        <div v-html="showHtml"></div>
        <h1 v-if="isIf">isIf是真的,显示了,false的情况下此节点删除</h1>
        <h1 v-else>我是isIf为false时显示</h1>
        <h1 v-show="isShow">isShow是真的,显示了,false的情况下此节点display为none</h1>
        <button @click="fun1">点击1</button>
        <button @dblclick="fun2">点击2</button>
        <div>
            <img v-bind:src="img1url" width="300px" height="100px" alt="">
        </div>
        <div>
            <form action="">
                <!-- 双向数据绑定是表单元素进行绑定 -->
                <input type="text" name="personNmae" v-model="person.name">
                <input type="text" name="age" v-model="person.age">
                <select name="love" v-model="person.love">
                    <option value="eat">吃</option>
                    <option value="drick">喝</option>
                </select>
                <input type="radio" v-model="person.sex" name="sex" value="1">男
                <input type="radio" v-model="person.sex" name="sex" value="2">女
                <div>
                    <select v-model="shabi1" name="" id="">
                        <option v-for="(item, index) in shabi" >{{item}}</option>
                    </select>
                </div>
                <div v-for="(attr,key) in person">
                    {{key}}-{{attr}}
                </div>
                <div>
                    <table border="1">
                        <tr>
                            <td>序号</td>
                            <td>姓名</td>
                            <td>年龄</td>
                            <td>操作</td>
                        </tr>
                        <tr v-for="(stu,index) in studentArray">
                            <td>{{index+1}}</td>
                            <td>{{stu.name}}</td>
                            <td>{{stu.age}}</td>
                            <td><button type="button" @click="fun3($event,stu.name)">修改</button></td>
                        </tr>
                    </table>
                </div>
            </form>
        </div>
        <div>
            <div @click="fun4">
                外层div
                <div @click.stop="fun4">里层div</div>
            </div>
        </div>
        <div>
            <a href="http://www.baidu.com" @click.prevent="fun5">百度</a>
        </div>
        <div>
            <div @click.once="fun6">一次事件</div>
        </div>
        <div>
            <!-- 13表示是输入enter,起的keycode值可以查询 -->
            <input type="text" @keyup.13="change">
        </div>
    </div>
</body>
<script src="../js/vue2.7.js"></script>
<script>
    // 创建一个Vue实例
    var app = new Vue({
        el:"#app", // 绑定一个元素
        data(){
            return {
                name:'小豪老放屁!!',
                isIf:true,
                isShow:true,
                img1url:'../img/banner1.jpg',
                person:{
                    name:'王导',
                    age:18,
                    love:'eat',
                    sex:1,
                },
                showHtml:'<h1>王导</h1>',
                shabi:["小豪","王导","光头"],
                shabi1:"小豪",
                studentArray:[
                    {name:"光头",age:"18"},
                    {name:"王导",age:"20"}
                ],
                times: 1690944730436,
                myclass:'yangshi yanse',
                isyangshi:true,
                isyanse:true,
                myclassObject:['yangshi','yanse']
            }
        },
        methods:{
            // 声明了一个叫fun1的方法
            fun1(){
                // 怎么在方法中使用data对象的变量,需要使用this
                console.log("button1被点击了",this.name);
                this.isShow=false
                if(this.isIf){
                    this.isIf=false
                }else{
                    this.isIf=true
                }
            },
            fun2(){
                console.log("button2被双击了");
            },
            fun3(event,name){
                console.log("事件对象",event);
                console.log(name);
            },
            fun4(event){
                console.log("111");
                // elent.stopPropagation(); // 使用原生js阻止冒泡
            },
            fun5(event){
                alert("111");
                // elent.preventDefault(); // 使用原生js阻止默认行为
            },
            fun6(){
                console.log("sss");
            },
            change(event){
                console.log(event.key,event.keyCode);
            }
        },
         // 2. 定义过滤器
        filters: {
            //3.定义一个处理函数,参数value为要处理数据,format为传入参数
            conversion: function (value, format) {
            //这个转换方法就不介绍了,看看就行,过滤器用法为主
            var date = new Date(value);
            function addZero(date) {
                if (date < 10) {
                return "0" + date;
                }
                return date;
            }
            let getTime = {
                yyyy: date.getFullYear(),
                yy: date.getFullYear() % 100,
                MM: addZero(date.getMonth() + 1),
                M: date.getMonth() + 1,
                dd: addZero(date.getDate()),
                d: date.getDate(),
                HH: addZero(date.getHours()),
                H: date.getHours(),
                hh: addZero(date.getHours() % 12),
                h: date.getHours() % 12,
                mm: addZero(date.getMinutes()),
                m: date.getMinutes(),
                ss: addZero(date.getSeconds()),
                s: date.getSeconds(),
                w: (function () {
                let a = ["日", "一", "二", "三", "四", "五", "六"];
                return a[date.getDay()];
                })(),
            };
            for (let i in getTime) {
                format = format.replace(i, getTime[i]);
            }
            return format;
            },
            //4.再定义一个过滤器,给数据前加上"时间为:"这几个字
            againChange: function (value) {
            return "时间为:" + value;
            },
        },
    })
</script>
</html>


96562a7c0df87a21da6f0e7a47b3093a_f1a53b7b01ce4eeb87798022f7b900c1.png


最后

送大家一句话:穷且益坚,不坠青云之志


相关文章
|
3天前
|
JavaScript
vue页面加载时同时请求两个接口
vue页面加载时同时请求两个接口
|
3天前
|
JavaScript
vue里样式不起作用的方法,可以通过deep穿透的方式
vue里样式不起作用的方法,可以通过deep穿透的方式
12 0
|
3天前
|
移动开发 JavaScript 应用服务中间件
vue打包部署问题
Vue项目`vue.config.js`中,`publicPath`设定为&quot;/h5/party/pc/&quot;,在线环境基于打包后的`dist`目录,而非Linux的`/root`。Nginx代理配置位于`/usr/local/nginx/nginx-1.13.7/conf`,包含两个相关配置图。
vue打包部署问题
|
3天前
|
JavaScript 前端开发
iconfont 图标在vue里的使用
iconfont 图标在vue里的使用
15 0
|
2天前
|
JavaScript 前端开发
vue2升级到vue3的一些使用注意事项记录(四)
vue2升级到vue3的一些使用注意事项记录(四)
|
2天前
|
JavaScript
vue打印v-model 的值
vue打印v-model 的值
|
3天前
|
JavaScript
Vue实战-组件通信
Vue实战-组件通信
5 0
|
3天前
|
JavaScript
Vue实战-将通用组件注册为全局组件
Vue实战-将通用组件注册为全局组件
5 0
|
3天前
|
JavaScript 前端开发
vue的论坛管理模块-文章评论02
vue的论坛管理模块-文章评论02
|
3天前
|
JavaScript Java
vue的论坛管理模块-文章查看-01
vue的论坛管理模块-文章查看-01