2019-09-28 18:50:56 +08:00
|
|
|
# Async
|
2020-10-23 15:01:01 +08:00
|
|
|
Dialog can be async.
|
2019-09-28 18:50:56 +08:00
|
|
|
```html
|
|
|
|
<n-button @click="handleClick">
|
|
|
|
Success
|
|
|
|
</n-button>
|
|
|
|
```
|
2019-10-21 18:20:00 +08:00
|
|
|
|
2019-09-28 18:50:56 +08:00
|
|
|
```css
|
|
|
|
.n-button {
|
|
|
|
margin: 0 12px 8px 0;
|
|
|
|
}
|
|
|
|
```
|
2019-10-21 18:20:00 +08:00
|
|
|
|
2019-09-28 18:50:56 +08:00
|
|
|
```js
|
2020-10-22 18:14:09 +08:00
|
|
|
const sleep = () => new Promise(resolve => setTimeout(resolve, 1000))
|
|
|
|
const countDown = second => `Count down ${second} second`
|
|
|
|
|
2019-09-28 18:50:56 +08:00
|
|
|
export default {
|
2020-10-22 18:14:09 +08:00
|
|
|
inject: [
|
|
|
|
'dialog'
|
|
|
|
],
|
2019-09-28 18:50:56 +08:00
|
|
|
methods: {
|
2019-10-21 18:20:00 +08:00
|
|
|
handleClick(e) {
|
2020-10-22 18:14:09 +08:00
|
|
|
const dialog = this.dialog.success({
|
2020-02-04 13:23:36 +08:00
|
|
|
title: 'Async',
|
2019-09-28 18:50:56 +08:00
|
|
|
content:
|
2020-02-04 13:23:36 +08:00
|
|
|
'Click and count down 3 second',
|
2020-02-05 23:49:59 +08:00
|
|
|
positiveText: 'Confirm',
|
2020-03-12 21:32:47 +08:00
|
|
|
onPositiveClick: () => {
|
2020-10-22 18:14:09 +08:00
|
|
|
dialog.loading = true
|
|
|
|
return new Promise(resolve => {
|
|
|
|
sleep()
|
|
|
|
.then(() => { dialog.content = countDown(2); return sleep() })
|
|
|
|
.then(() => { dialog.content = countDown(1); return sleep() })
|
|
|
|
.then(() => { dialog.content = countDown(0) })
|
|
|
|
.then(resolve)
|
|
|
|
})
|
2019-09-28 18:50:56 +08:00
|
|
|
}
|
2020-01-30 18:48:38 +08:00
|
|
|
})
|
2019-09-28 18:50:56 +08:00
|
|
|
}
|
|
|
|
}
|
2020-01-30 18:48:38 +08:00
|
|
|
}
|
2020-10-22 18:14:09 +08:00
|
|
|
```
|