页面跳转函数封装

我们进行H5页面跳转时,可以对跳转函数进行封装,如下面代码所示:

let jump = (str,...param)=>{
    str = str + ".html"
    let count = 0
    param.forEach((item,index)=>{
        if(item.indexOf("null")==-1){
            if(count == 0){
                str += "?" + item 
                count++
            }else{
                str += "&" + item
            }
        }
    })
    location.href = str
}

此函数有两个亮点语法:

1.此函数通过...param可传多个参数,即跳转文件名和不定数量的url参数。调用方式为jump("index")或jump("index","a=1")或jump("index","a=1","b=2")

2.为了简化循环,我们可以用let...of,但是这样获取不了index,所以我们选用arr.forEach((item,index)=>{})进行循环处理。

原文地址:https://www.cnblogs.com/luoyihao/p/14579717.html