1、获取url里边的参数
const getQueryVariable = (variable: string) => { const query = window.location.search.substring(1); const vars = query.split('&'); for (const item of vars) { const pair = item.split('='); if (pair[0] === variable) { return pair[1]; } } return 0; };
用法: getQueryVariable("id");
2、类似于loash.js的get函数
const get = (source: any, path: string, defaultValue: any) => { const paths = path.replace(/[(d+)]/g, '.$1').split('.'); let result = source; for (const p of paths) { result = Object(result)[p]; if (result === undefined) { return defaultValue; } } return result; };
用法:get(object,object.name,0);第一个参数是需要查找的对象,第二个参数是需要查找的key,最后一参数是如果没找到返回的默认值。