基于 Promise 的对话 vue js?

Aji*_*gde 4 javascript plugins vue.js

我创建了一个插件,但我不知道如何创建一个基于 Promise 的插件。你能告诉我我需要在现有代码中添加什么吗?

我使用 vuetify js 进行材质样式

NotifyDlg.vue:包含警报或确认对话的对话代码。根据消息类型,我将显示/隐藏按钮

<template>
  <v-dialog max-width="500px"
            v-model='dialogue'>
    <v-card>
      <v-card-title primary-title>
        <v-icon :color="messageType">{?{messageType}}</v-icon>
        <title>{?{title}}</title>
      </v-card-title>
      <v-card-text>{?{message}}</v-card-text>
      <v-card-actions>
        <v-spacer></v-spacer>
        <v-btn :color="messageType"
               flat
               v-if="confirmDlg"
               @click="value=true">Yes</v-btn>
        <v-btn :color="confirmDlg?'':'primary'"
               flat
               @click="value=false">{?{getBtnText()}}</v-btn>
      </v-card-actions>
    </v-card>
  </v-dialog>
</template>
<script>
export default {
  props: {
    confirmDlg: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '',
      required: true
    },
    message: {
      type: String,
      default: '',
      required: true
    },
    messageType: {
      type: String,
      default: 'warning',
      required: true
    }
  },
  data () {
    return {
      value: false,
      dialogue:false
    }
  },
  methods: {
    getBtnText () {
      if (this.confirmDlg) return 'No'
      return 'Ok'
    }
  }
}
</script>
Run Code Online (Sandbox Code Playgroud)

NotifyDlgPlugin.js:插件安装代码。然后将使用 Vue.Use 方法在 main.js 中调用此方法。

import NotifyDlg from './NotifyDlg.vue'

export default {
  install: (Vue, options) => {
    Vue.prototype.$notifyDlg = {
      show (message, title, messageType, options = {}) {
        options.message = message
        options.title = title
        options.messageType = messageType
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

从文档中我只了解了可以在 install 方法中调用的全局函数。但我不明白如何调用我创建的对话或如何将 true 或 false 值返回到被调用的方法。

对我的问题有什么建议吗?

Bob*_*ger 7

我想分享我的基于承诺的对话框代码:

import Dialog from "./Dialog.vue";

export function confirm(title, message) {
  return new Promise((resolve, reject) => {
    const dialog = new Vue({
      methods: {
        closeHandler(fn, arg) {
          return function() {
            fn(arg);
            dialog.$destroy();
            dialog.$el.remove();
          };
        }
      },
      render(h) {
        return h(Dialog, {
          props: {
            title,
            message,
          },
          on: {
            confirm: this.closeHandler(resolve),
            cancel: this.closeHandler(reject, new Error('canceled'))
          }
        });
      }
    }).$mount();
    document.body.appendChild(dialog.$el);
  });
}
Run Code Online (Sandbox Code Playgroud)

这将创建对话框,将其添加到 DOM 并在 Dialog 触发this.$emit('confirm')事件时进行解析。