简体   繁体   English

如何在 Vue 项目中使用 npm 设置 firebase v6.3.4?

[英]How to setup firebase v6.3.4 using npm in Vue project?

I need to use Firebase in my Vue.js project and after installing it by npm install firebase --save I made a new folder in root-src named 'firebase'.我需要在我的 Vue.js 项目中使用 Firebase,在通过npm install firebase --save后,我在 root-src 中创建了一个名为“firebase”的新文件夹。 In there I have one .js file - 'init.js'.在那里我有一个 .js 文件 - 'init.js'。 Inside of that .js file I want to setup my firebase.在那个 .js 文件中,我想设置我的 firebase。 Code below shows my full 'init.js' file.下面的代码显示了我完整的“init.js”文件。 Then in one of my .vue files when I want to access it I need to 1st do import db from '@/firebase/init' .然后在我的 .vue 文件之一中,当我想访问它时,我需要首先import db from '@/firebase/init'

I get one big red error over my screen only.我的屏幕上只有一个大的红色错误。

 const firebaseConfig = {
     apiKey: "...",
     authDomain: "...",
     databaseURL: "...",
     projectId: "...",
     storageBucket: "...",
     messagingSenderId: "..",
     appId: "..." }

 const firebaseApp = firebase.initializeApp(firebaseConfig)

 firebaseApp.firestore().settings({ timestampsInSnapshots: true})

 export default firebaseApp.firestore()

The big red error says that it大红色错误说它

Failed to compile - Error:ENOENT编译失败 - 错误:ENOENT

I think the easiest solution is to install vue-fire.我认为最简单的解决方案是安装 vue-fire。 You have a lot documentation about configuration here:您在这里有很多关于配置的文档:

Vue-Fire - Getting Started Vue-Fire - 入门

and also basic example here:以及这里的基本示例:

Vue-Fire - Basic Example Vue-Fire - 基本示例


If you do not want to use it you should read this great article:如果你不想使用它,你应该阅读这篇很棒的文章:

Real-World Web App With Vue.js and Firebase (there is also a repo of this project) 带有 Vue.js 和 Firebase 的真实世界 Web 应用程序(还有这个项目的 repo)

Good luck :)祝你好运 :)

You don't seem to be importing firebase in init.js, so your use of the firebase variable there does not refer to anything.您似乎没有在 init.js 中导入 firebase,因此您在那里使用的 firebase 变量没有参考任何内容。

import firebase from 'firebase/app'

// And your code...
 const firebaseConfig = {
     apiKey: "...",
     authDomain: "...",
     databaseURL: "...",
     projectId: "...",
     storageBucket: "...",
     messagingSenderId: "..",
     appId: "..." }

 const firebaseApp = firebase.initializeApp(firebaseConfig) // consume firebase here

 firebaseApp.firestore().settings({ timestampsInSnapshots: true})

 export default firebaseApp.firestore()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM