Vue 函数封装

  1. 格式化浏览器时间

    /**
    * 格式化时间
    * @param params
    * @param blo 默认为true
    * @returns {string}
    * @constructor 冯刚 2019年6月12日11点01分
    */
    function TimeConversion(params,blo=true){
    var stamp = Date.parse(params);
    var newDate= new Date(stamp);
    var year = newDate.getFullYear();
    var month = newDate.getMonth() + ;
    var date = newDate.getDate();
    var h = newDate.getHours();
    var m = newDate.getMinutes();
    var s = newDate.getSeconds();
    if(blo)
    return year + '-' + getNow(month) + "-" + getNow(date);
    return year + '-' + getNow(month) + "-" + getNow(date) + " " + getNow(h) + ':' + getNow(m) + ":" + getNow(s); }
  2. 校验字符串最后是否存在斜杠
    /**
    * 验证最后是否有反斜杠
    * @param value
    * @returns {*}
    * @constructor 冯刚 2019年6月12日
    */
    function Verification(value) {
    if (value.length > && value !== '') {
    var str = value.substr(value.length - , );
    if (str !== '/' && str !== '') {
    value += '/';
    }
    }
    return value;
    }
  3. 字符串加密
    /**
    * 加密
    * @param code 要加密的字符串
    * @returns {string}
    */
    function compileStr(code) {
    var c = String.fromCharCode(code.charCodeAt() + code.length);
    for (var i = ; i < code.length; i++) {
    c += String.fromCharCode(code.charCodeAt(i) + code.charCodeAt(i - ));
    }
    return escape(c);
    }
  4. 字符串解密
    /**
    * 解密
    * @param code 要解密的字符串
    * @returns {string}
    */
    function uncompileStr(code) {
    code = unescape(code);
    var c = String.fromCharCode(code.charCodeAt() - code.length);
    for (var i = ; i < code.length; i++)
    c += String.fromCharCode(code.charCodeAt(i) - c.charCodeAt(i - ));
    return c;
    }
  5. 根据key获取浏览器地址后参数
    /**
    * js获取url传递指定参数,解决url中带中文乱码的问题(根据key获取value)
    * @param key
    * @returns {string|null}
    */
    function getQueryString(key) {
    var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr().match(reg);
    if (r !== null) return decodeURI(r[]); return null;
    }
  6. 文本框非空校验,文本框添加红色样式
    /**
    * 文本框非空验证
    * @param type 自定义类型(.或#)
    * @param name 页面中自定义类型名称
    * @author 冯刚 2019年6月12日
    */
    function isNotNull(type, name) {
    var temp, select;
    var isNull = false;
    if (checkValue(type, name))
    isNull = true;
    temp = type + name;
    if (!isNull)
    $($(temp)).each(function () {
    var _this = $(this);
    _this = reductionStyle(_this);
    temp = _this.children('input').val();
    select = _this.children('div').children('input').val();
    if (temp === '' || temp === null) {
    isNull = true;
    _this.children('input').css('border', 'solid red 1px');
    }
    if (select === '' || select === null) {
    isNull = true;
    _this.children('div').children('input').css('border', 'solid red 1px');
    }
    });
    return isNull;
    }
  7. 重置初始化样式
    /**
    * 重置初始化样式
    * @param type
    * @param name
    * @returns {boolean}
    */
    function resetStyle(type, name) {
    var temp;
    var isBool = false;
    if (checkValue(type, name))
    isBool = true;
    temp = type + name;
    if (!isBool)
    $($(temp)).each(function () {
    var _this = $(this);
    _this = reductionStyle(_this);
    isBool = true;
    });
    return isBool;
    }
  8. 数据封装成 data 对象,并且去除空格
    /**
    * 封装数据
    * @param data 数据对象(用于添加修改)部分可用
    */
    function packData(data){
    var vmData={};
    for (var o in data) {
    if (data[o] !== null && data[o] instanceof Array)
    vmData[o]=null;
    else{
    if(typeof data[o] === 'string' && data[o].length > )
    vmData[o]=data[o].trim();
    else
    vmData[o]=data[o];
    }
    }
    return vmData;
    }
  9. 动态绑定数据
    /**
    * 动态赋值(用于查看编辑)
    * @param orgObj
    * @param newObj
    * @returns {*}
    * @constructor fg 2019年6月12日
    */
    function AssignmentObject(orgObj, newObj){
    for (var o in orgObj) {
    if (!(orgObj[o] !== null && orgObj[o] instanceof Array))
    {
    for (var n in newObj){
    if(o==n){
    orgObj[o]=newObj[n];
    break;
    }
    }
    }
    }
    return orgObj;
    }
  10. 清空文本框内容,重置内容
    /**
    * 按钮重置内容
    * @param data
    */
    function clearContent(data) {
    var v_data = {};
    for (var o in data) {
    if (data[o] !== null && data[o] instanceof Array)
    v_data[o] = data[o];
    else {
    v_data[o] = null;
    }
    }
    return v_data;
    }
  11. 部分函数校验
    /**
    * 内部引用 还原样式
    * @param obj
    * @returns {*}
    */
    function reductionStyle(obj) {
    obj.children('input').css('border', '1px solid #dcdfe6');
    obj.children('div').children('input').css('border', '1px solid #dcdfe6');
    return obj;
    } /**
    * 内部引用 检测选择器以及类型名称是否输入全
    * @param key
    * @param value
    * @returns {boolean}
    */
    function checkValue(key, value) {
    var isBool = false;
    if (isCheck(key) && isCheck(value)) {
    isBool = true;
    alert('请检查选择器类型及名称');
    }
    if (isCheck(key)) {
    isBool = true;
    alert('选择器类型不能为空:(./#)');
    }
    if (isCheck(value)) {
    isBool = true;
    alert('选择器名称不能为空');
    }
    return isBool;
    } /**
    * 内部引用 校验值是否为空
    * @param value
    * @returns {boolean}
    */
    function isCheck(value) {
    value = value.trim();
    if (value === null || value === '' || value === 'undefined')
    return true;
    return false;
    }
  12. 获取当前时间
    /**
    * 校验时间
    * @param s
    * @returns {string}
    */
    function getNow(s) {
    return s < ? '' + s : s;
    } /**
    * 获取当前时间
    * @returns {string}
    */
    function getDate() {
    var myDate = new Date();
    var year = myDate.getFullYear();
    var month = myDate.getMonth() + ;
    var date = myDate.getDate();
    var h = myDate.getHours();
    var m = myDate.getMinutes();
    var s = myDate.getSeconds();
    return year + '-' + getNow(month) + "-" + getNow(date) + " " + getNow(h) + ':' + getNow(m) + ":" + getNow(s);
    }

    作者地址:https://www.cnblogs.com/FGang/p/11124070.html

vue 封装公用函数的更多相关文章

  1. vue封装公用弹出框方法,实现点击出现操作弹出框

    vue封装公用弹出框方法,实现点击出现操作弹出框 如上图所示,这次要实现一个点击出现操作弹框的效果:并将这个功能封装成一个函数,便于在项目的多个地方使用. 具体思路是: 封装一个组件,组件保护一个插槽 ...

  2. vue防抖节流函数---组件封装,防止按钮多次点击

    1.vue 封装utils.js /** * @param {function} func 执行函数 * @param {number} time 防抖节流时间 * @param {boolean} ...

  3. vue 防抖节流函数——组件封装

    防抖(debounce) 所谓防抖,就是指触发事件后在 n 秒内函数只能执行一次,如果在 n 秒内又触发了事件,则会重新计算函数执行时间. 节流(throttle) 所谓节流,就是指连续触发事件但是在 ...

  4. vue封装插件并发布到npm上

    vue封装插件并发布到npm上 项目初始化 首先,要创建项目,封装vue的插件用webpack-simple很合适,vue init webpack-simple 项目名称此命令创建我们的项目的目录, ...

  5. 浅析vue封装自定义插件

    在使用vue的过程中,经常会用到Vue.use,但是大部分对它一知半解,不了解在调用的时候具体做了什么,因此,本文简要概述下在vue中,如何封装自定义插件. 在开始之前,先补充一句,其实利用vue封装 ...

  6. jQuery编写插件--封装全局函数的插件(一些常用的js验证表达式)

    上一篇写到了jQuery插件的3种类型,介绍了第一种类型的写法--封装jQuery对象的方法插件.这一篇要介绍第二种插件类型:封装全局函数的插件:这类插件就是在jQuery命名空间内部添加函数:这类插 ...

  7. delphi公用函数

    {*******************************************************} { } { Delphi公用函数单元 } { } { 版权所有 (C) 2008 } ...

  8. wemall app商城源码Android之支付宝接口公用函数

    wemall-mobile是基于WeMall的Android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享wemall app商城源码Android之  ...

  9. 使用promise手动封装ajax函数

    最近在做一个单页应用,node和浏览器仅通过json传输数据,因为是只有自己用等于是锻炼一下自己,所以也不用考虑seo的问题,node端我已经写好了,但是浏览器端想要用ajax原生太麻烦,用封装的函数 ...

随机推荐

  1. ajax 简单例子

    Html 代码: <html> <body> <div id="myDiv"><h3>Let AJAX change this te ...

  2. Spring Cloud微服务安全实战_3-4_API安全机制之认证

     认证:登录和认证是 两个概念,比如你两周.一个月,可能只登录了一次,但认证却是每次访问都要经过的步骤. 对于图中的认证不成功,也要继续处理,这个我觉得得看业务,比如管理系统,不登录就不让你访问,但对 ...

  3. selenium--多窗口操作

    前戏 想一想,我们为什么要获取窗口句柄呢?有什么用呢? 来假设一下,我们打开了一个网站,点击了一个按钮,新打开了一个页面,我们在新页面操作完成之后,需要回到原来的页面继续操作,这时候你如果继续操作原来 ...

  4. haproxy 配置文件详解 之 综述

    HAProxy 配置文件根据功能和用途,主要有5 个部分组成,但有些部分并不是必须的,可以根据需要选择相应的部分进行配置. 1.global 部分 用来设定全局配置参数,属于进程级的配置,通常和操作系 ...

  5. R 语言解压目录下的所有gz文件

    setwd("GSE29431_RAW") # 进入目录 fileNames <- list.files() # 获取目录下的所有文件 sapply(fileNames, g ...

  6. 【python学习案例】python判断自身是否正在运行

    需要引入psutil包: 实现思路: 1)用os.getpid()获取当前程序运行PID,将PID存入文件中 2)用psutil模块获取当前系统所有正在运行的pid 3)读取之前存入的PID,判断该P ...

  7. spring boot 启动遇到报错:Failed to configure a DataSource

    spring  boot 启动遇到报错,具体如下 Description: Failed to configure a DataSource: 'url' attribute is not speci ...

  8. 使用vue搭建应用五引入Mock.js

    为了模拟后台接口提供页面所需的数据,引入Mock.js Mock.js是一个模拟数据生成器,可帮助前端开发和原型与后端进度分开 特性: 前后端分离 增加单元测试的真实性 数据类型丰富 方便扩展 1.安 ...

  9. 记录一次在生成数据库服务器上出现The timeout period elapsed prior to completion of the operation or the server is not responding.和Exception has been thrown by the target of an invocation的解决办法

    记一次查询超时的解决方案The timeout period elapsed...... https://www.cnblogs.com/wyt007/p/9274613.html Exception ...

  10. mysql创建存储过程动态SQL语句

    DROP PROCEDURE IF EXISTS x.`wk`; DELIMITER $$ CREATE PROCEDURE `x`.`wk`() BEGIN ); ); SET t = CONCAT ...