common-all笔记-vuesell

释放双眼,带上耳机,听听看~!

common/data.js:

export function formatDate(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);
}

common/store.js:

export function saveToLocal(id, key, value) {
let seller = window.localStorage.seller;
if (!seller) {
seller = {};
seller[id] = {};
} else {
seller = JSON.parse(seller);
if (!seller[id]) {
seller[id] = {};
}
}
seller[id][key] = value;
window.localStorage.seller = JSON.stringify(seller);
};

export function loadFromLocal(id, key, def) {
let seller = window.localStorage.seller;
if (!seller) {
return def;
}
seller = JSON.parse(seller)[id];
if (!seller) {
return def;
}
let ret = seller[key];
return ret || def;
};

common/util.js:
/**

  • 解析url参数
  • @example ?id=12345&a=b
  • @return Object {id:12345,a:b}
    */
    export function urlParse() {
    let url = window.location.search;
    let obj = {};
    let reg = /[?&][^?&]+=[^?&]+/g;
    let arr = url.match(reg);
    // [‘?id=12345’, ‘&a=b’]
    if (arr) {
    arr.forEach((item) => {
    let tempArr = item.substring(1).split(‘=’);
    let key = decodeURIComponent(tempArr[0]);
    let val = decodeURIComponent(tempArr[1]);
    obj[key] = val;
    });
    }
    return obj;
    };

【转自慕课】https://www.imooc.com

Vue.js

vue2.0中组件传值理解

2022-3-3 19:46:11

Vue.js

Vue构建单页应用最佳实战-2.X版

2022-3-3 19:46:37

搜索