admin管理员组文章数量:1434916
// Initialize Firebase
var config = {
apiKey: "...",
authDomain: "my-project.firebaseapp",
databaseURL: "",
projectId: "my-project",
storageBucket: "my-project.appspot",
messagingSenderId: "..."
};
firebase.initializeApp(config);
When I initialize Firebase in index.html or main.js, in both cases i have an error
Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app).
I have to initialize it in my ponent (Dashboard). Is it proper way to initialize firebase in one of my ponents? Why i can do this in e.g. main.js ?
// Initialize Firebase
var config = {
apiKey: "...",
authDomain: "my-project.firebaseapp.",
databaseURL: "https://my-project.firebaseio.",
projectId: "my-project",
storageBucket: "my-project.appspot.",
messagingSenderId: "..."
};
firebase.initializeApp(config);
When I initialize Firebase in index.html or main.js, in both cases i have an error
Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app).
I have to initialize it in my ponent (Dashboard). Is it proper way to initialize firebase in one of my ponents? Why i can do this in e.g. main.js ?
Share Improve this question edited Nov 6, 2018 at 20:48 aBiscuit 4,7421 gold badge19 silver badges29 bronze badges asked Nov 6, 2018 at 20:15 lukluk 1391 gold badge2 silver badges9 bronze badges 1- Similar asked question: stackoverflow./questions/53139432/… – user10297237 Commented Nov 6, 2018 at 22:19
2 Answers
Reset to default 2One way is to have a firebaseConfig.js
file like the following
import firebase from 'firebase/app';
import 'firebase/firestore';
var config = {
apiKey: "....",
authDomain: ".....",
databaseURL: ".....",
......
};
firebase.initializeApp(config);
const db = firebase.firestore();
// date issue fix according to firebase
const settings = {
timestampsInSnapshots: true
};
db.settings(settings);
export {
db
};
and import and use it in each of your Components (where you need Firebase) as follows:
<template>
......
</template>
<script>
const firebase = require('../firebaseConfig.js');
export default {
name: 'xxxxx',
data() {
return {
....
};
},
methods: {
fetchData() {
firebase.db
.collection('xxxxx')
.get()
..... //Example of a call to the Firestore database
}
}
....
};
</script>
So I found that creating an index.js
within a firebase
directory.
Then within that file you should set it up with the following:
import firebase from "firebase/app";
import "firebase/firestore";
import 'firebase/auth';
// firebase init - add your own config here
const firebaseConfig = {
apiKey: '',
authDomain: '',
databaseURL: '',
projectId: '',
storageBucket: '',
messagingSenderId: '',
appId: ''
}
firebase.initializeApp(firebaseConfig)
// utils
const db = firebase.firestore()
const auth = firebase.auth()
// collection references
const usersCollection = db.collection('users')
const postsCollection = db.collection('posts')
// export utils/refs
export {
db,
auth,
usersCollection,
postsCollection
}
Then inside of main.js
should be the following
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import { auth } from './firebase'
let app
auth.onAuthStateChanged(() => {
if (!app) {
app = new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
}
})
本文标签: javascriptWhere to initialize firebase in Vue js appStack Overflow
版权声明:本文标题:javascript - Where to initialize firebase in Vue js app? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745622697a2666783.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论