简体   繁体   English

VueJS + Firebase 如何显示图像?

[英]VueJS + Firebase How do I display an image?

I want to display an image stored in Firebase, but it doesn't work.我想显示存储在 Firebase 中的图像,但它不起作用。 Currently, nothing is displayed when accessing the page.目前,访问页面时不显示任何内容。 Where are the mistakes?错误在哪里?

[The task] [任务]

  1. Get id from URL (... /: id)从 URL 获取 id (... /: id)
  2. Get image url from Firestore从 Firestore 获取图像 url
    Document name = id文档名称 = id
  3. View image看图
<template>
  <v-container>
    <v-row align="center" justify="center">
      <v-col cols="12" md="8" xs="12">
        <img :src="imageurl" />
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
import firebase from "firebase";
var db = firebase.firestore();

export default {
  data() {
    return {
      imageurl: ""
    };
  },
  mounted() {
    const id = this.$route.params.id;

    let cityRef = db.collection("cards").doc(id);
    let getDoc = cityRef.get();
    this.imageurl = getDoc;
  }
};
</script>

The following should work:以下应该有效:

mounted() {
    const id = this.$route.params.id;
    const cityRef = db.collection("cards").doc(id);

    cityRef.get().then(doc => {
        if (doc.exists) {
            console.log(doc.data()) 
            this.imageurl = doc.data().imageurl
        } else {
            console.log('no data')
        }
    })
    .catch(error => {
        //.....
    }
}

As explained in Vue - Cannot set property of undefined in promise , since "you are using the keyword function , this is inside its scope the anonymous function, not the vue instance". As explained in Vue - Cannot set property of undefined in promise , since "you are using the keyword function , this is inside its scope the anonymous function, not the vue instance".

By using an arrow function you will solve the error.通过使用箭头 function 您将解决错误。


See also the comments I've made on the other answer: by calling the asynchronous get() method you get a Promise that "resolves with a DocumentSnapshot containing the current document contents".另请参阅我对另一个答案所做的评论:通过调用异步get()方法,您将获得一个 Promise ,它“使用包含当前文档内容的DocumentSnapshot解析”。 On the DocumentSnapshot you have to call the data() method to "retrieve all fields in the document as an Object".DocumentSnapshot上,您必须调用data()方法来“将文档中的所有字段作为对象检索”。 Then you have to assign the value of one of those fields, like this.imageurl = doc.data().imageurl;然后您必须分配其中一个字段的值,例如this.imageurl = doc.data().imageurl;

cityRef.get() returns a promise, so you should get image that way: cityRef.get()返回一个 promise,所以你应该这样得到图像:

cityRef.get().then(function(doc) {
 if (doc) {
  console.log(doc.data()) // doc.data() is a response from your query
  this.imageurl = doc.data().imageurl
 } else {
  console.log('no data')
 }
} catch(error) {
...
}

There you have documentation那里有 文档

You need to change the syntax to load firebase and to get the doc:您需要更改语法以加载 firebase 并获取文档:

import firebase from 'firebase/app'
import firebaseConfig from './config/firebase' .  // Your firebase config init settings.
...

db.collection('cards').doc(id)
.get().then(docSnapshot => {
    if (!docSnapshot.exists) return;
    let data = docSnapshot.data()
    // continue your code
});

PS: I see you use the new Vuetify 2, great! PS:我看到你使用新的 Vuetify 2,太棒了!

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

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