tim*_*mue 13 firebase vue.js vuex
我想在我的Vue.JS应用程序中集成Firebase.
我想知道在哪里把引用放到Firebase.
Vam*_*hna 40
将firebase作为项目中的依赖项安装cd
到项目目录中,并在命令行中运行以下命令
npm install --save firebase
Run Code Online (Sandbox Code Playgroud)
现在在你的main.js文件中添加这个
import Vue from 'vue'
import App from './App.vue'
import * as firebase from 'firebase'
import { store } from './store/store'
const config = {
apiKey: "xxxxxxx",
authDomain: "xxxxxxx",
databaseURL: "xxxxxxx",
storageBucket: "xxxxxxxx",
messagingSenderId: "xxxxxxx"
};
firebase.initializeApp(config);
Vue.prototype.$firebase = firebase;
new Vue({
el: '#app',
store,
render: h => h(App)
})
Run Code Online (Sandbox Code Playgroud)
火力-config.js
export const config = {
apiKey: "xxxxxxx",
authDomain: "xxxxxxx",
databaseURL: "xxxxxxx",
storageBucket: "xxxxxxxx",
messagingSenderId: "xxxxxxx"
};
Run Code Online (Sandbox Code Playgroud)
现在在main.js中执行如下操作
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)
})
Run Code Online (Sandbox Code Playgroud)
将firebase添加到Vue.prototype
允许使用的vue组件中使用firebasethis.$firebase
如果你不想要这种行为,你可以使用初始化firebase firebase.initializeApp(config);
现在进入您的vuex商店,您可以导入firebase模块,如下所示
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()
}
}
});
Run Code Online (Sandbox Code Playgroud)致@umang建议将firebase
全局命名空间添加到firebaseVue.prototype
应用程序实例.
归档时间: |
|
查看次数: |
5820 次 |
最近记录: |