z-util-page
引入
1. 全局引入
拷贝包目录下dist文件夹内 [ zutilpage.min.js ] 文件到自己的项目里,在HTML里添加如下引用:
<script src="zutilpage.min.js"></script>
这会添加一个全局变量 [ Utils ] 到window对象;
Utils.debounce(function(){
console.log('身体和心灵,总有一个在路上。');
}, 200);
2. 按需引入
运行以下命令将工具包安装到本地
npm i z-util-page --save
根据需要自行引入
import { debounce, throttle, deepClone } from 'z-util-page';
说明:
1. 函数防抖
import { debounce } from 'z-util-page'
let debounced = debounce(function(){
console.log('身体和心灵,总有一个在路上。');
return '身体和心灵,总有一个在路上。';
}, 1000, true);
debounced.then(function(res){
console.log(res);
});
debounced();
2. 函数节流
import { throttle } from 'z-util-page'
let throttle = throttle(function(){
console.log('身体和心灵,总有一个在路上。');
return '身体和心灵,总有一个在路上。';
}, 1000, {
leading: true,
trailing: true
});
3. 深拷贝
import { deepClone } from 'z-util-page'
let newValue = deepClone({
a: '身体和心灵,总有一个在路上。',
b: {
c: new Date(),
d: [1, 3, 4],
e: Symbol(),
a: null,
b: undefined,
f: {
a: 1,
b: true,
}
},
c: document.createElement('div'),
d: new RegExp(/\d+/ig),
e: new Error('错误'),
f: function(){
console.log('身体和心灵,总有一个在路上。');
}
});
4. 生成UUID
import { generateUUID } from 'z-util-page'
generateUUID();
5. Web端文件操作
import { FileHelper } from 'z-util-page';
FileHelper.choose(function(files){
console.log(files);
},{
accept: [".doc",".docx","application/msword"],
capture: "user",
multiple: true
});
const fileReader = FileHelper.read(file)
.loadend(function (res) {
console.log(res);
})
.error(function(err){
console.log(err);
})
.start('Text');
fileReader.stop();
fileReader.getStatus();
fileReader.getResult();
FileHelper.save(new Blob([JSON.stringify({a: '身体和心灵,总有一个在路上。'}, null, 2)], {type : 'application/json'}), 'test.json');
6. Http
import { Http } from 'z-util-page';
const http = new Http({
timeout: 10000,
baseUrl: "https://developer.mozilla.org",
contentType: 'application/json',
responseType: 'json'
});
http.ajax({
url: '/test',
method: 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE',
type: "arraybuffer" | "blob" | "document" | "json" | "text",
timeout: 100,
data: {
name: 'jack'
},
header: {
'Content-Type': 'multipart/form-data'
},
file: {
'file': new Blob([JSON.stringify({a: '身体和心灵,总有一个在路上。'}, null, 2)], {type : 'application/json'})
}
}).then((res)=>{
console.log(res);
});
7. Reactive
import { Reactive } from 'z-util-page';
const a = {
text: ""
};
const b = Reactive.reactive({
text: ""
});
Reactive.effect(function () {
a.text = b.text;
});
b.text = "jack";
console.log(a.text);
8. CookieHelper
import { CookieHelper } from 'z-util-page';
CookieHelper.getItem('TEST');
CookieHelper.getItemOnce('TEST');
CookieHelper.setItem('TEST', '身体和心灵,总有一个在路上。');
CookieHelper.removeItem('TEST');
CookieHelper.exist('TEST');
CookieHelper.clear('TEST');