【问题标题】:unresolved variable $firebaseRefs vue js未解析的变量$firebaseRefs vue js
【发布时间】:2020-04-29 13:50:00
【问题描述】:

我有一个添加函数可以将电影添加到 firebase 数据库

methods: {
            add() {
                this.$firebaseRefs.movies.push({
                    name: this.newItem.name,
                    price: this.newItem.genre,
                    rating: this.newItem.rating,
                    reviews: this.newItem.reviews,
                    cast: this.newItem.cast,

                });
                this.newItem.name = '';
                this.newItem.genre = '';
                this.newItem.rating = '';
                this.newItem.reviews= '';
                this.newItem.cast= '';
                this.$router.push('/dashboard')
            }
        }
    }

但是我收到一个错误Unresolved variable $firebaseRefs 并且当我尝试将路线更改添加到 http://localhost:8080/add?

我已经导入了

import { db } from '../db';

db.js

import * as firebase from "firebase";
import store from "./store";
let config={
    apiKey: "AIzaSyDQ2dXBMuIJ2EBYeVqucJpOF33C0tsFlLk",
    authDomain: "tv-show-tracker-9899e.firebaseapp.com",
    databaseURL: "https://tv-show-tracker-9899e.firebaseio.com",
    projectId: "tv-show-tracker-9899e",
    storageBucket: "tv-show-tracker-9899e.appspot.com",
    messagingSenderId: "433917891798",
    appId: "1:433917891798:web:bb35a74ae42e6db339a577"
};
let app = firebase.initializeApp(config);
export const db = app.database();

// eslint-disable-next-line no-unused-vars
let firebaseRefs = db.ref('movies');

firebase.auth().onAuthStateChanged(user => {
    store.dispatch("fetchUser", user);
});

下面是main.js文件

main.js

import Vue from 'vue'
import App from './App.vue'
import router from "./routes/route.js";

import store from "./store";



Vue.config.productionTip = false;




new Vue({
  router,
  store,
  render: h => h(App),
}).$mount('#app')

【问题讨论】:

  • 您好,您需要分享更多详细信息以便我们为您提供帮助,尤其是您安装了哪个plugin 以及如何安装它,以及imports 您在你的组件中有。
  • 我已经编辑了问题
  • 好的,谢谢。您能否也分享您的main.js 文件以查看您是如何导入插件的?
  • @RenaudTarnec 添加了

标签: firebase vue.js vuefire


【解决方案1】:

如 vuefire documentation 中所述,您需要将 Vuefire 作为 Vue 插件安装。因此,由于您使用实时数据库,您需要将您的main.js 文件修改如下

import Vue from 'vue'
import App from './App.vue'
import router from "./routes/route.js";
import store from "./store";
import { rtdbPlugin } from 'vuefire'

Vue.use(rtdbPlugin)
Vue.config.productionTip = false;

new Vue({
  router,
  store,
  render: h => h(App),
}).$mount('#app')

别忘了按如下方式安装最新版本的 Vuefire(参见文档):

npm install vuefire

【讨论】:

  • 在我的 db.js 中怎么样?
  • 我也应该导入它吗?
  • 如果我理解正确,您写道您在组件中执行import { db } from '../db';。应该没问题。如果你不确定,你可以分享你的 Vue 组件的全部内容。
  • @kadzokanene 很高兴能为您提供帮助,并感谢您接受答案。如果您认为它有帮助,您也可以点赞。见stackoverflow.com/help/someone-answers
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多