常用的代码片段

截取url地址栏的参数(单个参数)

getUrlParam(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
                var r = window.location.search.substr(1).match(reg);  //匹配目标参数
                if (r != null) return unescape(r[2]); return null; //返回参数值
            }

截取url多个参数

function GetRequest(url) {
        // var url = location.search; //获取url中"?"符后的字串
        var theRequest = {};
        if (url.indexOf("?") != -1) {
            var str = url.substring(url.indexOf("?")+1);
            strs = str.split("&");
            for(var i = 0; i < strs.length; i ++) {
                theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
            }
        }
        // console.log(theRequest);
        return theRequest;
    }
    var url = 'https://www.baidu.com/s?id=111&name=yourname';
    GetRequest(url)

传递index值 

axios拦截器(请求头拦截,设置token值)

axios.interceptors.request.use(function (config) {
    // 取出localStorage中存储的token值
    let token = 你的token值
    // 设置到请求头中 Authorization这个名字是后台规定的
    config.headers['_gt'] = token
    return config
}, function (error) {
    return Promise.reject(error)
})

px转rem

document.documentElement.style.fontSize = document.documentElement.clientWidth / 7.5*2 + 'px';//设置1rem=100px

猜你喜欢

转载自blog.csdn.net/Arbort_/article/details/81774475