Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: $dialog对话框可以异步 #7

Merged
merged 1 commit into from
Apr 8, 2022
Merged

Conversation

liulinboyi
Copy link

@liulinboyi liulinboyi commented Apr 8, 2022

在这个例子中,使用
对话框可以异步时,需要将

const NDialog = useDialog() // 的结果

NDialog返回

使用原版dialog,如下:

<template>
  <n-button @click="handleClick">
    成功
  </n-button>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import { useDialog } from 'naive-ui'

const sleep = () => new Promise((resolve) => setTimeout(resolve, 1000))
const countDown = (second: number) => `倒计时 ${second} 秒`

export default defineComponent({
  setup () {
    const dialog = useDialog()
    return {
      handleClick () {
        const d = dialog.success({
          title: '异步',
          content: '点击,倒计时 3 秒',
          positiveText: '确认',
          onPositiveClick: () => {
            d.loading = true
            return new Promise((resolve) => {
              sleep()
                .then(() => {
                  d.content = countDown(2)
                  return sleep()
                })
                .then(() => {
                  d.content = countDown(1)
                  return sleep()
                })
                .then(() => {
                  d.content = countDown(0)
                })
                .then(resolve)
            })
          }
        })
      }
    }
  }
})
</script>

使用封装的$dialog,如下:

<template>
  <n-button @click="handleClick">
    成功
  </n-button>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import { useDialog } from 'naive-ui'

const sleep = () => new Promise((resolve) => setTimeout(resolve, 1000))
const countDown = (second: number) => `倒计时 ${second} 秒`

export default defineComponent({
  setup () {
    const dialog = useDialog()
    return {
      handleClick () {
        const d = $dialog.success( '异步', {
          content: '点击,倒计时 3 秒',
          positiveText: '确认',
          onPositiveClick: () => {
            d.loading = true
            return new Promise((resolve) => {
              sleep()
                .then(() => {
                  d.content = countDown(2)
                  return sleep()
                })
                .then(() => {
                  d.content = countDown(1)
                  return sleep()
                })
                .then(() => {
                  d.content = countDown(0)
                })
                .then(resolve)
            })
          }
        })
      }
    }
  }
})
</script>

@zclzone
Copy link
Owner

zclzone commented Apr 8, 2022

Very useful, thank you.

@zclzone zclzone merged commit ea9851c into zclzone:main Apr 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants