当前位置: 首页 > 科技观察

可以使用JS工具函数

时间:2023-03-21 16:24:19 科技观察

Vue3引入constoDiv=document.createElement('div');constoScript=document.createElement('script');oDiv.setAttribute('id','app');oScript。type='text/javascript';oScript.src="https://unpkg.com/vue@next";document.body.appendChild(oDiv);document.body.appendChild(oScript);窗口.onload=function(){const{createApp,ref}=Vue;constApp={template:`

{{msg}}

{{count}}

`,data(){return{msg:'maomin'}},setup(){letcount=ref(0);return{count}}}createApp(App).mount('#app');}递归搜索操作(以删除指定项为例)//recursivesearchforrecursion(data,id){letresult;if(!data){return;}for(leti=0;i0){result=this.recursion(item.childrenBranch,id);if(result){returnresult;}}}returnresult;},递归数组,设置数组为空为undefinedfunctionuseTree(data){for(letindex=0;indexret)ret=length;}}returnret;},vardata=[{addr:"1",isNew:false,},{addr:"2",isNew:false,}]max(group(data)//2检测版本为vue3import{h}from'vue';constisVue3=typeofh==='function';console.log(isVue3)检测是否有空对象letarr=[{},{name:'1'}]constarr=this.bannerList.filter(item=>item==null||item==''||JSON.stringify(item)=='{}');console.log(arr.length>0?'notpassed':'passed')deepcopy/*@param{*}obj*@param{Array}cache*@return{*}*/functiondeepCopy(obj,cache=[]){//justreturnifobjisimmutablevalueif(obj===null||类型eofobj!=='object'){returnobj}//ifobjishit,itisincircularstructureconsthit=find(cache,c=>c.original===obj)if(hit){returnhit.copy}constcopy=Array.isArray(obj)?[]:{}//先将复制放入缓存中//因为我们要引用递归深度复制缓存.push({original:obj,copy})Object.keys(obj).forEach(key=>{copy[key]=deepCopy(obj[key],cache)})returncopy}constobjs={name:'maomin',age:'17'}console.log(deepCopy(objs));h5文字转语音speech(txt){varsynth=null;varmsg=null;synth=window.speechSynthesis;msg=newSpeechSynthesisUtterance();msg.text=txt;msg.lang="zh-CN";synth.speak(msg);if(window.speechSynthesis.speaking){console.log("音效有效");}else{console.log("音效失效");}}模糊搜索recursion(data,name){letresult;if(!data){return;}for(vari=0;i-1){result=item;break;}elseif(item.children&&item.children.length>0){result=this.recursion(item.children,name);if(result){returnresult;}}}returnresult;},onSearch(v){if(v){if(!this.recursion(this.subtable,v)){this.$message({type:'error',message:'Search小于',});}else{this.tableData=[this.recursion(this.subtable,v)];}}},输入数字类型channelInputLimit(e){letkey=e.key;//不允许输入e'and'.'if(key==='e'||key==='.'){e.returnValue=false;returnfalse;}returntrue;},排序(交换位置)constlist=[1,2,3,4,5,6];functionuseChangeSort(arr,oldIndex,newIndex){consttargetRow=arr.splice(oldIndex,1)[0];consttargetRow1=arr.splice(newIndex,1)[0];arr.splice(newIndex,0,targetRow);arr.splice(oldIndex,0,targetRow1);returnarr}console.log(useChangeSort(list,5,0));//[6,2,3,4,5,1]格式格式化时间/***Parsethetimetostring*@param{(Object|string|number)}time*@param{string}cFormat*@returns{string|null}*/exportfunctionparseTime(time,cFormat){if(arguments.length===0||!time){returnnull;}constformat=cFormat||'{y}-{m}-{d}{h}:{i}:{s}';letdate;if(typeoftime==='object'){date=time;}else{if(typeoftime==='string'){if(/^[0-9]+$/.test(时间)){//support"1548221490638"time=parseInt(time);}else{//supportsafari//https://stackoverflow.com/questions/4310953/invalid-date-in-safaritime=time.replace(newRegExp(//-/gm),'/');}}if(typeoftime==='number'&&time.toString().length===10){time=time*1000;}date=newDate(time);}constformatObj={y:date.getFullYear(),m:date.getMonth()+1,d:date.getDate(),h:date.getHours(),i:date.getMinutes(),s:date.getSeconds(),a:date.getDay()};consttime_str=format.replace(/{([ymdhisa])+}/g,(result,key)=>{constvalue=formatObj[key];//注意:getDay()returns0onSundayif(key==='a'){return['day','one','two','three','four','five','six'][value];}returnvalue.toString().padStart(2,'0');});returntime_str;}本文转载自微信《前端之路》,可通过以下二维码关注e.转载本文请联系前端公众号。