I've been working on creating a VueJS plugin that will allow me to call a global method, triggering a popup message with an input text field. My goal is to be able to use the following code snippet in any Vue component:
this.$customPopup("Title","Body","Value");
Upon calling this method, a popup should appear on the screen.
I attempted to build it, but encountered an issue where this.$refs
was not recognized when called within the install() function:
DeleteConfirmation.vue
<template>
<b-modal size="lg" ref="deleteConfirmationModal" :title="this.title" header-bg-variant="danger" @ok="confirmDelete" @cancel="confirmCancel">
<p>
{{this.body}}
</p>
</b-modal>
</template>
<script>
export default {
data()
{
return {
title: null,
body: null,
valueCheck: null,
value: null
};
},
install(vue, options)
{
Vue.prototype.$deleteConfirmation = function(title, body, expectedValue)
{
this.title = title;
this.body = body;
this.valueCheck = expectedValue;
this.$refs.$deleteConfirmation.show()
}
},
}
</script>
app.js
import DeleteConfirmation from './components/global/DeleteConfirmation/DeleteConfirmation';
Vue.use(DeleteConfirmation);
The call I am attempting to make is:
$vm0.$deleteConfirmation("title","body","val");
During runtime, I encounter the following error:
app.js?id=c27b2799e01554aae7e1:33 Uncaught TypeError: Cannot read property 'show' of undefined
at Vue.$deleteConfirmation (app.js?id=c27b2799e01554aae7e1:33)
at <anonymous>:1:6
Vue.$deleteConfirmation @ app.js?id=c27b2799e01554aae7e1:33
(anonymous) @ VM1481:1
It seems that this.$refs
in DeleteConfirmation.vue is undefined.