其他分享
首页 > 其他分享> > 项目中可能用到的一些方法

项目中可能用到的一些方法

作者:互联网

1.获取url里面的参数值或者追加查询字符串

  假如我们有这样一个url,"?post=1234&action=edit",可以这样处理这个url

var urlParams = new URLSearchParams('?post=1234&action=edit');

console.log(urlParams.has('post')); 
console.log(urlParams.get('action')); // "edit"
console.log(urlParams.getAll('action')); // ["edit"]
console.log(urlParams.toString()); // "?post=1234&action=edit"
console.log(urlParams.append('active', '1')); // "?post=1234&action=edit&active=1"

2.函数直接赋值一个函数,如果没有传参,我们就直接抛出错误提醒,如果一个组件里面有很多方法,我们就可以直接复用,而不用每个方法里面都去做非空判断处理

const isRequired = () => { throw new Error('param is required'); };

const hello = (name = isRequired()) => { console.log(`hello ${name}`) };
// 抛出一个错误,因为没有参数没有传
hello();
// 没有问题
hello('hello')

 

标签:urlParams,log,项目,edit,用到,action,console,post,方法
来源: https://www.cnblogs.com/musi03/p/10794223.html