import Vue from 'vue'
import App from './App.vue'
import * as firebase from 'firebase'
import { store } from './store/store'
import { config } from './firebase-config'
firebase.initializeApp(config);
Vue.prototype.$firebase = firebase;
new Vue({
el: '#app',
store,
render: h => h(App)
})
adding firebase to the Vue.prototype allows usage of firebase in your vue components by using this.$firebase
if you don't want this behavior you can just initialize firebase using firebase.initializeApp(config);
now coming to your vuex store you can just import the firebase module as below
import Vue from 'vue'
import Vuex from 'vuex'
import * as firebase from 'firebase'
Vue.use(Vuex);
export const store = new Vuex.Store({
state:{},
mutations:{},
actions:{
myFirebaseAction: ({commit}) => {
//you can use firebase like this
var ref = firebase.database().ref()
}
}
});
Credits to @umang for suggesting to add the firebase global namespace to the Vue.prototype instead of the firebase app instance.
To install firebase as a dependency in your project
cd
into your project directory and run the following command in the command lineNow in your main.js file add this
firebase-config.js
Now in your main.js do as follows
adding firebase to the
Vue.prototype
allows usage of firebase in your vue components by usingthis.$firebase
if you don't want this behavior you can just initialize firebase using
firebase.initializeApp(config);
now coming to your vuex store you can just import the firebase module as below
Credits to @umang for suggesting to add the
firebase
global namespace to theVue.prototype
instead of the firebase app instance.