// Initialize Firebase
var config = {
apiKey: "...",
authDomain: "my-project.firebaseapp.com",
databaseURL: "https://my-project.firebaseio.com",
projectId: "my-project",
storageBucket: "my-project.appspot.com",
messagingSenderId: "..."
};
firebase.initializeApp(config);
当我在 index.html 或 main.js 中初始化 Firebase 时,在这两种情况下我都会出错
Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app).
我必须在我的组件(仪表板)中初始化它。在我的一个组件中初始化 firebase 是正确的方法吗?为什么我可以这样做主.js ?
最佳答案
一种方法是使用如下所示的 firebaseConfig.js
文件
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
};
并在您的每个组件(您需要 Firebase 的地方)中导入并使用它,如下所示:
<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>
关于javascript - 在 Vue js 应用程序中在哪里初始化 firebase?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53179341/