<button @click="handleConfirm">提交</button>
接下来直接调用`getInfoByModal`即可使用模态框,轻松获取用户填写的数据。
这也是很多UI组件库中对常用组件的一种封装方式。
### 3. async/await的另类用法
很多人只知道在`async函数`调用时用`await`接收返回值,但不知道`async函数`其实就是一个返回promise的函数,例如下面两个函数是等价的:
const fn1 = async () => 1;
const fn2 = () => Promise.resolve(1);
fn1(); // 也返回一个值为1的promise对象
而`await`在大部分情况下在后面接promise对象,并等待它成为fullfilled状态,因此下面的fn1函数等待也是等价的:
await fn1();
const promiseInst = fn1();
await promiseInst;
**然而,await还有一个鲜为人知的秘密**,当后面跟的是非promise对象的值时,它会将这个值使用promise对象包装,因此await后的代码一定是异步执行的。如下示例:
Promise.resolve().then(() => {
console.log(1);
});
await 2;