javascript - 尝试将 firebase 配置导入我的 vue 文件
问题描述
编辑:添加错误^^!https://i.imgur.com/72x6LQX.png
firebase.js?5b23:11 Uncaught TypeError: app.database is not a function
at eval (firebase.js?5b23:11)
at Object../src/components/firebase/firebase.js (app.js:3496)
at __webpack_require__ (app.js:679)
at fn (app.js:89)
at eval (selector.js?type=script&index=0!./src/components/front-end/Getstarted.vue:3)
at Object../node_modules/babel-loader/lib/index.js!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/components/front-end/Getstarted.vue (app.js:2791)
at __webpack_require__ (app.js:679)
at fn (app.js:89)
at eval (Getstarted.vue?5f4d:1)
at Object../src/components/front-end/Getstarted.vue (app.js:3552)
所以我试图将firebase代码放在一个不同的文件中,而不是我使用它的.vue文件。如果我把它放在vue文件中,它确实像预期的那样工作,所以那里没有问题。但是我似乎无法弄清楚如何正确地做到这一点。
<script>
import Firebase, { functions } from 'firebase'
import conf from '../firebase/firebase'
let config = {
apiKey: ''
authDomain: ''
databaseURL: ''
projectId: ''
storageBucket: ''
messagingSenderId: ''
}
let app = Firebase.initializeApp(config)
let db = app.database()
let booksRef = db.ref('books')
(dashboard.Vue)
*这就是我的vue文件中的样子*
我有 2 个 javascript 文件,其中一个包含:
import config from './firebaseConfig'
class Firebase {
constructor () {
firebase.initializeApp(config)
}
}
let db = app.database()
let booksRef = db.ref('books')
export default Firebase()
(firebase.js)
和一个:
let config = {
apiKey: '',
authDomain: '',
databaseURL: '',
projectId: '',
storageBucket: '',
messagingSenderId: '',
appId: ''
}
export default (config)
(firebaseConfig.js)
我的文件夹结构如下所示:
components
|firebase
||firebase.js
||firebaseConfig.js
|backend
||Dashboard.vue
当我加载网页时,它只是一个空白页。想知道如何正确构建它,如果有任何不清楚的地方,请告诉我
解决方案
在下面的行中,您没有声明 variable app
,所以它是undefined
let db = app.database()
您可以更改firebase.js
为
import firebase from 'firebase'
import config from './firebaseConfig'
const app = firebase.initializeApp(config)
let db = app.database()
export default db
并从您的组件中
import db from './firebase.js'
let booksRef = db.ref('books')
...
推荐阅读
- docker - 如何避免多个计划任务并行运行
- sql - 如何将具有排名值的列转换为oracle中的行
- dictionary - 通过使用 Haskell 中的 Data.Map 获取键值来查找键
- javascript - 这个递归函数如何改变“历史”变量?
- python - 我应该使用多线程将工作表记录复制到另一个工作表吗?
- javascript - 暂时禁用 javascript 中的特定 css 样式
- node.js - 上传文件 Api 请求在邮递员中有效,但在 nodejs 中无效,并对 MERN 做出反应
- c# - 调试时 RedirectToAction 不起作用
- php - 如果 php 引擎失败,laravel / codeigniter 项目文件会显示为文本吗?
- jenkins - 重启android终端后詹金斯卡住了