工作随记

  • 20210512   ?. 和 ??的用法

  这种写法在判断空值时,很好用 

1. ?. 

let res = obj?.data?.list

// 等价 

let res = obj && obj.data && obj.data.list

2. ??  相当于设置默认值

console.log(1 ?? "xx") //1
console.log(0 ?? "xx") //0
console.log(null ?? "xx") //xx
console.log(undefined ?? "xx") //xx
console.log(-1 ?? "xx") //-1
console.log("" ?? "xx") 
  •  20210527 async await 处理异常的写法,挺好用的
// 处理异常信息
const awaitWrap = (promise) => {
   return promise.then((res) => [null, res]).catch((err) => [err, null]);
};
const [err, res] = await awaitWrap(save(params));

 3. 方法中返回一个promise时,调用的时候要用try catch包裹下,不然方法返回错误时,会报错  Error in v-on handler (Promise/async): "false" 

工作随记

 1 validateMechod() {
 2     const _this = this;
 3     return new Promise(function (resolve, reject) {
 4        _this.$refs.form.validate((valid) => {
 5         if (!valid) {
 6           reject(false);
 7         } else {
 8           resolve(_this.form);
 9         }
10       });
11     });
12 } 
// try{
  //  this.validateMechod()
  //} catch(error){
  //  console.error(error)
//}