2022年9月

/**
 * 异步Computed
 * @param compute 异步计算函数
 * @param initValue 初始值
 * @returns Ref 一个只读的异步计算Ref
 */
export function asyncComputed(compute,initValue){
    const value = shallowRef(initValue);
    watchEffect(async ()=>{
        console.log('trigger update')
        value.value = await compute();
    })
    return readonly(value);
}
const asyncValue = asyncComputed(()=>{
    return new Promise((resolve)=>{
        setTimeout(()=>{
            resolve(1)
        },2000})
})

//输出undefined
console.log(asyncValue.value);
//2秒后变成1

 

//?? 等价于 isset
$a = '';
$b = isset($a) ? $a : 'hahha';
$b = $a ?? 'hahha';
//$b is ''
//适用于多层级array的属性读取
$arr = [ 'son'=>['name'=>'child'] ];
echo $arr['son']['age'] ?? '19';
//output: 19
echo $arr['non']['name'] ?? 'non child';
//output: non child

//?: 等价于empty
$c = empty($a) ? 'hahaha' : $a;
$c = $a ?: 'hahaha';
//c is 'hahaha'