【发布时间】:2018-10-24 03:34:35
【问题描述】:
我创建了一个插件,但我不知道如何创建一个基于 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>
NotifyDlgPlugin.js:插件安装代码。然后这个方法会在 main.js 中使用 Vue.Use 方法调用。
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
}
}
}
}
从文档中我了解到只有可以在 install 方法中调用的全局函数。但我不明白如何调用我创建的对话或如何将真假值返回给被调用的方法。
对我的问题有什么建议吗?
【问题讨论】:
标签: javascript vue.js plugins