2

我不了解如何将 firebase 数据导入 vue ......一旦我得到答案,我很高兴删除这个问题或辞去我作为程序员的工作;)

我知道数据是异步的,所以 vue 试图在接收到数据之前渲染组件。但是,我不知道如何让它等待。示例将不胜感激。我试过阅读文档,但我只是不明白。

这是一个很小的应用程序,它显示我们公司的停车场并将用户的水龙头(汽车)位置保存到火力基地(我们厌倦了忘记我们停在哪里)。感谢您的帮助!

哦,我使用 set() 是因为一次只需要保存一个汽车位置,所以每次用户点击屏幕上的新位置时,数据都会被覆盖。

<template>
  <div id="app">
    <span id="marker" class="fas fa-times" :style="{ left: leftCoord, top: topCoord }"></span>
    <img @click="saveCoordinates($event)" src="./assets/parking-lot.jpg">
  </div>
</template>

<script>
import firebase from 'firebase'

const config = {
  apiKey: 'MY-API-KEY',
  authDomain: 'MY-APP.firebaseapp.com',
  databaseURL: 'https://MY-APP.firebaseio.com',
  projectId: 'MY-APP',
  storageBucket: 'MY-APP.appspot.com',
  messagingSenderId: '1234567890'
}
const app = firebase.initializeApp(config)

const db = app.database()
const locationRef = db.ref('location')

export default {
  name: 'app',
  firebase: {
    location: locationRef
  },
  mounted () {
    this.leftCoord = this.location.leftCoord
    this.topCoord = this.location.topCoord
  },
  data () {
    return {
      leftCoord: '',
      topCoord: ''
    }
  },
  methods: {
    saveCoordinates: function (clickEvent) {
      const coordinates = {
        leftCoord: clickEvent.layerX,
        topCoord: clickEvent.layerY
      }
      this.location.set(coordinates)
    }
  }
}
</script>
4

3 回答 3

2

这可以解决问题:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">

    <script src="https://www.gstatic.com/firebasejs/4.11.0/firebase.js"></script>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vuefire/dist/vuefire.js"></script>
</head>
<body>


<div id="app">

    <span id="marker" class="fas fa-times" :style="{ left: leftCoord, top: topCoord }"></span>
    <img @click="saveCoordinates($event)" src="./assets/parking-lot.jpg">

    <img @click="readCoords" src="./assets/parking-lot.jpg">

</div>

<script>

    var config = {
        apiKey: ".........",
        authDomain: ".........",
        databaseURL: ".........",
        projectId: ".........",
        storageBucket: "........."
    };


    /* global Vue, firebase */
    var db = firebase.initializeApp(config).database()
    var todosRef = db.ref('todos')

    const locationRef = db.ref('location')

    new Vue({
        el: '#app',
        name: 'app',
        firebase: {
            location: {
                source: locationRef,
                asObject: true
            }

        },
        mounted() {

            locationRef.once('value', snapshot => {
                this.leftCoord = this.location.leftCoord
                this.topCoord = this.location.topCoord
            })

        },
        data() {
            return {
                leftCoord: '',
                topCoord: ''
            }
        },
        methods: {
            saveCoordinates: function (clickEvent) {
                console.log(clickEvent.layerX)
                const coordinates = {
                    leftCoord: clickEvent.layerX,
                    topCoord: clickEvent.layerY
                }
                locationRef.set(coordinates)
            },

            readCoords: function (clickEvent) {
                console.log(this.location.leftCoord);
                console.log(this.location.topCoord);
            }
        }
    })


</script>
</body>
</html>

显然 vuefire 中没有“本机”方式来等待加载数据的挂载钩子,请参阅https://github.com/vuejs/vuefire/issues/69

我添加了第二个带有新功能的图像链接,readCoords仅用于测试目的。


请注意,您也可以在安装的钩子中执行

locationRef.once('value', snapshot => {
    this.leftCoord = snapshot.val().leftCoord
    this.topCoord = snapshot.val().topCoord
})
于 2018-05-15T15:22:49.580 回答
1

我添加了第二个答案,它不再基于mounted钩子,而是基于对象绑定的回调函数。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">

    <script src="https://www.gstatic.com/firebasejs/4.11.0/firebase.js"></script>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/vuefire/dist/vuefire.js"></script>
</head>
<body>


<div id="app">

    <span id="marker" class="fas fa-times" :style="{ left: leftCoord, top: topCoord }"></span>
    <img @click="saveCoordinates($event)" src="./assets/parking-lot.jpg">

</div>

<script>

    var config = {
        apiKey: ".........",
        authDomain: ".........",
        databaseURL: ".........",
        projectId: ".........",
        storageBucket: "........."
    };


    /* global Vue, firebase */
    var db = firebase.initializeApp(config).database()
    var todosRef = db.ref('todos')

    const locationRef = db.ref('location')

    new Vue({
        el: '#app',
        name: 'app',
        firebase: {
            location: {
                source: locationRef,
                asObject: true,
                readyCallback: function () {
                    this.leftCoord = this.location.leftCoord
                    this.topCoord = this.location.topCoord
                }
            }

        },
        data() {
            return {
                leftCoord: '',
                topCoord: ''
            }
        },
        methods: {
            saveCoordinates: function (clickEvent) {
                console.log(clickEvent.layerX)
                const coordinates = {
                    leftCoord: clickEvent.layerX,
                    topCoord: clickEvent.layerY
                }
                locationRef.set(coordinates)
            }
        }
    })


</script>
</body>
</html>
于 2018-05-15T15:49:03.633 回答
1

如果 location 是一个对象,你需要像这样绑定:

export default {
  name: 'app',
  firebase: {
    location: {
      source: db.ref('location'),
      asObject: true,
      readyCallback: function () {}
    }
  },
  mounted () {
    this.leftCoord = this.location.leftCoord
    this.topCoord = this.location.topCoord
  },
  data () {
    return {
      leftCoord: '',
      topCoord: ''
    }
  },
  methods: {
    saveCoordinates: function (clickEvent) {
      const coordinates = {
        leftCoord: clickEvent.layerX,
        topCoord: clickEvent.layerY
      }
      this.$firebaseRefs.location.set(coordinates)
    }
  }
}

注意,设置的时候,我们需要使用this.$firebaseRefs.location.set(coordinates)

文档https://github.com/vuejs/vuefire

于 2018-05-15T15:15:47.387 回答