Archived
2
0
This repository has been archived on 2024-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
2020-04-05 03:17:06 +00:00

59 lines
1.4 KiB
TypeScript

import Vue from 'vue'
import { SweetAlertOptions } from 'sweetalert2'
import Swal from 'sweetalert2/dist/sweetalert2.js'
type VueSwalInstance = typeof Swal.fire
declare module 'vue/types/vue' {
interface Vue {
$swal: VueSwalInstance
}
interface VueConstructor<V extends Vue = Vue> {
swal: VueSwalInstance
}
}
interface VueSweetalert2Options extends SweetAlertOptions {
// includeCss?: boolean;
}
class VueSweetalert2 {
static install(vue: Vue | any, options?: VueSweetalert2Options): void {
const swalFunction = (...args: [SweetAlertOptions]) => {
if (options) {
const mixed = Swal.mixin(options)
return mixed.fire.apply(mixed, args)
}
return Swal.fire.apply(Swal, args)
}
let methodName: string | number | symbol
for (methodName in Swal) {
// @ts-ignore
if (Object.prototype.hasOwnProperty.call(Swal, methodName) && typeof Swal[methodName] === 'function') {
// @ts-ignore
swalFunction[methodName] = ((method) => {
return (...args: any[]) => {
// @ts-ignore
return Swal[method].apply(Swal, args)
}
})(methodName)
}
}
vue['swal'] = swalFunction
// add the instance method
if (!vue.prototype.hasOwnProperty('$swal')) {
vue.prototype.$swal = swalFunction
}
}
}
export default VueSweetalert2