I have a question, so in react we have react consumer and provider, that let pass variables wherever i want. Does Vue has something like that? I need pass my Firebase class to almost every component, so pass it through props is a bad idea.
Asked
Active
Viewed 207 times
0
-
1Use global variable like vuex or vuestash – Balaji Nov 30 '19 at 09:52
-
Have a look at [vuefire](https://github.com/vuejs/vuefire/tree/master/packages/vuefire) – Ohgodwhy Nov 30 '19 at 10:03
-
Duplicate of https://stackoverflow.com/questions/40896261/apply-global-variable-to-vuejs – Michal Levý Nov 30 '19 at 15:50
1 Answers
1
You can create a wrapper plugin for any module (ie. firebase in this case).
/plugins/firebase-wrapper.js
import firebase from 'firebase'
const FirebaseWrapper = {
install (Vue) {
// INSTALL
if (this.installed) return
this.installed = true
// CONFIG
const config = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: ""
}
// INIT
firebase.initializeApp(config)
Vue.prototype.$firebase = firebase
Vue.firebase = firebase
}
}
export default FirebaseWrapper
main.js
import FirebaseWrapper from "./plugins/firebase-wrapper.js"
Vue.use(FirebaseWrapper)
Access firebase APIs from a .vue file like below
let currentUser = await this.$firebase.auth().currentUser
Or
import Vue from 'vue'
let currentUser = await Vue.firebase.auth().currentUser
vuefire (npm module) possibly will do the same for you, ie. will create a wrapper plugin & will make firebase APIs globally available, so you won't have to write your own custom plugin.

Shivam Singh
- 1,671
- 1
- 11
- 25