vue设置公共方法:把毫秒值换为yyyy-mm-dd hh:mm等方法

本文介绍了一种在Vue.js项目中封装公共JS方法的方法,包括特殊字符转义、日期格式化、语音播放等实用功能,使代码更加模块化,便于复用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

封装一些vue的公共js方法:

步骤:

第一:在main.js中加入

import commonjs from './assets/js/common.js'//引入公共js
Vue.prototype.commonjs = commonjs;//加入以后就可以全局调了

第二:在组件里加入

this.commonjs.formatDate(data.data,'yyyy-MM-dd h:m');//formatDate是公共方法名

第三:我的公共js

const synth = window.speechSynthesis
const msg = new SpeechSynthesisUtterance()
export default{
  /**
   * 特殊字符转义公共方法
   */
    escEncode(str){
      str = str.replace(/\%/g,'%25').replace(/\&/g,'%26').replace(/\#/g,'%23');
      str = str.replace(/\+/g,'%2B').replace(/\//g,'%2F').replace(/\\/g,'%5C');
      str = str.replace(/\=/g,'%3D').replace(/\?/g,'%3F').replace(/\ /g,'%20');
      str = str.replace(/\./g,'%2E').replace(/\:/g,'%3A');
      return str;
    },
  /**
   * 日期转换公共方法
   */
  formatDate(date,format){
      var o = {
           "M+" : date.getMonth()+1,                 //月份
           "d+" : date.getDate(),                    //日
           "h+" : date.getHours(),                   //小时
           "m+" : date.getMinutes(),                 //分
           "s+" : date.getSeconds(),                 //秒
           "q+" : Math.floor((date.getMonth()+3)/3), //季度
           "S"  : date.getMilliseconds()             //毫秒
       };
       if(/(y+)/.test(format)) {
               format=format.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
       }
        for(var k in o) {
           if(new RegExp("("+ k +")").test(format)){
                format = format.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
            }
        }
       return format;
   },
  /**
   * 把毫秒值换为yyyy-mm-dd hh:mm
   */
  getNewDate(date,format){
    var oDate = new Date(date),
        oYear = oDate.getFullYear(),
        oMonth = oDate.getMonth()+1,
        oDay = oDate.getDate(),
        oHour = oDate.getHours(),
        oMin = oDate.getMinutes(),
        oSen = oDate.getHours(),
        oTime = oYear+'-'+this.addZero(oMonth)+'-'+this.addZero(oDay),
        oTimeT = oYear+'-'+this.addZero(oMonth)+'-'+this.addZero(oDay)+" "+this.addZero(oHour)+':'+this.addZero(oMin);
        if(format == 'yyyy-MM-dd h:m'){
          return oTimeT;
        }
        if(format == 'yyyy-MM-dd'){
          return oTime;
        }
  },
  addZero(num){//补0
    if(parseInt(num) < 10){
      num = '0'+num;
    }
    return num;
  },
  handleSpeak(e) {//播放语音
    msg.text = e;
    msg.lang = 'zh-CN';
    msg.volume = '1';//播放音量0-1
    msg.rate = 1;//播放语速0.1-10
    msg.pitch = 1;//音调高低0-2
    synth.speak(msg)
  },
  handleStop(e) {//取消语音
    msg.text = e;
	  msg.lang = 'zh-CN';
    synth.cancel(msg)
  },
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

浩星

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

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

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

打赏作者

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

抵扣说明:

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

余额充值