记录一下vue项目中的常用方法

记录一下vue项目中的常用方法,第1张

记录一下vue项目中的常用方法 时间格式化
<span>{{d.createdDate | formatTimer}}span>
 filters: {
        formatTimer: function(value) {
          let date = new Date(value);
          let y = date.getFullYear();
          let MM = date.getMonth() + 1;
          MM = MM < 10 ? "0" + MM : MM;
          let d = date.getDate();
          d = d < 10 ? "0" + d : d;
          let h = date.getHours();
          h = h < 10 ? "0" + h : h;
          let m = date.getMinutes();
          m = m < 10 ? "0" + m : m;
          let s = date.getSeconds();
          s = s < 10 ? "0" + s : s;
          return y + "-" + MM + "-" + d + " " + h + ":" + m;
        }
      },

格式化后渲染出来如下:
2022-5-12 15:56

封装格式化时间方法
创建一个js文件formatDate.js,内容如下
//方法一
export function formatDate(val) {
    var date = new Date(Number(val)); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + "-";
    var M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
    var D = date.getDate() + " ";
    var h = date.getHours() + ":";
    var m = date.getMinutes() + ":";
    var s = (date.getSeconds() < 10 ? "0" + (date.getSeconds()) : date.getSeconds());
    return Y + M + D + h + m + s;
}

//方法二
export function formatDates(date, fmt) {
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    };
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            let str = o[k] + '';
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
        }
    }
    return fmt;
};

function padLeftZero(str) {
    return ('00' + str).substr(str.length);
}

 import {formatDate,formatDates} from '@/utils/formatDate'
 export default {
    data() {
      return {
        newsList: [],
       time: "1581672605401"
        page: 1,
        size: 2,
        tag:'标签测试'
      }
    },
    created () {
      this.getNewsList()
    },
    filters: {
          //方法一
      fmtime(val) {
      return fmdata(val);
    }//使用封装中的方法二
      formatTime(val){
        let data=new Date(val)
        return  formatDates(data,'yyyy-MM-dd hh:mm');
      }
      },
    methods: { },
    },

  }

2020-02-14 17:30
2020-11-18 16:49

模拟a标签事件`
		let a = document.createElement('a');//创建a标签
        a.href = '';//添加跳转路径
        a.target = '_self';//target属性 _blank 打开新的页面
        a.click(); //模拟点击
身份z验证
 cardID(){
        var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; 
           if(!reg.test( this.form.sfz)){ 
             Toast.fail('身份z输入格式不正确'); 
             this.form.sfz = ''
           }
    },
手机号验证
  mobile(){
          const regCN = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
          if (!regCN.test( this.form.phone)) {
            Toast.fail('手机号格式不正确');
            this.form.phone=''
          }
      },
修改数组对象的键值名
     /**
         * arr:数组
         * key:新key名
         * replaceKey:原始key名
         **/
         replaceObjectKey(arr, key, replaceKey) {
            let newArr = [];
            arr.forEach((item, index) => {
                for (var i = 0; i < key.length; i++) {
                item[key] = item[replaceKey];
                }
                // 删除原始key名
                delete item[replaceKey];
                newArr.push(item);
            });
            return newArr;
        },

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/945099.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-18
下一篇2022-05-18

发表评论

登录后才能评论

评论列表(0条)

    保存