I'm currently working on developing a Vue plugin aimed at simplifying the management of authentication state throughout my application. This particular plugin will require interaction with other Vue plugins such as vuex
, vue-router
, and vue-apollo
(for the time being).
I initially attempted to extend Vue.prototype
, but encountered issues when trying to access the plugin's properties in the usual way - for example, using this.$apollo
. This resulted in an error due to the scope of the object being incorrect, leading to an undefined
response. Another approach I tried was adding vm = this
and utilizing vm.$apollo
, yet this only shifted the scope further out without reaching the Vue
object itself, possibly because there was no instance of the Vue object available at that point.
export const VueAuth = {
install (Vue, _opts) {
Vue.prototype.$auth = {
test () {
console.log(this.$apollo)
}
}
}
}
(The remaining plugins are imported and incorporated via Vue.use()
within the main app.js
)
An alternative approach I explored involved...
// ...
install (Vue, { router, store, apollo })
// ...
However, being relatively new to JavaScript, I am uncertain about how this method operates regarding passing copies of objects versus mutating the originals or passing by reference. Additionally, this explicit technique may result in more overhead if the plugin needs to interact with additional plugins in the future.
Could someone offer suggestions on an efficient and organized manner to achieve this? Should I consider modifying an instance of Vue
instead of the prototype?