【问题标题】:Vuejs Firebase read PropertyVuejs Firebase 读取属性
【发布时间】:2021-01-25 08:16:09
【问题描述】:
<template>
<div id="app">
<h1 id="title">{{ quiz.title }}</h1>
  <div id="ques" v-for="(question, index) in quiz.questions" :key="question.text">
    <div v-show="index === questionIndex">
    <div id="tFlex">
      <p id="indexStuff">{{index+1}}</p>
      <h2 id="quest">{{ quiz.question.text }}</h2>
    </div>
      <ol>
        <li id="choices" v-for="response in question.responses" :key="response.text">
          <label>
            <input id="responce" type="radio" v-bind:value="response.correct" v-bind:name="index" v-model="userResponses[index]">
            <span class="t">{{response.text}}</span>
          </label>
        </li>
      </ol>
      <div id="btns">
        <button id="prevbtn" v-if="questionIndex > 0" v-on:click="prev">Previous Question</button>
        <button id="nxtbtn" v-on:click="next">Next Question</button>
      </div>
    </div>
  </div>
  <div v-show="questionIndex === quiz.questions.length">
    <h2 id="fint">Quiz finished</h2>
    <p id="sct">Total score: {{ score() }} / {{ quiz.questions.length }}</p>
    <div id="btnFlex"> 
      <button id="chck" v-show="questionIndex === quiz.NumQuestions" v-on:click="check">Check Answers</button>
    </div>
  </div>
</div>
</template>

<script>
import { db } from './firebase';

const DefaultPath = 'quiz/01';

var Quiz;

db.doc(DefaultPath).get().then((doc) => {
  Quiz = doc.data();
});

const quiz = Quiz;

console.log(quiz);

export default {
  data() {
    return{
      quiz:quiz,
      questionIndex: 0,
      canDo: true,
      userResponses:Array(quiz.questions.length).fill(false),
    }
  },
  methods: {
  next: function() {
    this.questionIndex++;
  },
  prev: function() {
    this.questionIndex--;
  },
  score: function() {
    return this.userResponses.filter(function(val) { return val }).length;
  },
  check: function() {
      this.questionIndex = 0;
      this.canDo = false;
      this.$forceUpdate();
  },
  cando: function() {
    return {canDo:!this.canDo};
  },
}, 
}
</script>

<style>
@import './style.css';
</style>

我对 Vue.js 有一个奇怪的问题。我对 Vue.js 和 web firebase 非常陌生。

我在 Chrome 开发者工具中给出了这个奇怪的错误。我正在尝试使用 Firebase 制作一个测验应用程序。我也尝试过先做 Firestore 的事情,然后做数据功能。我是 vuejs 的新手。

【问题讨论】:

    标签: javascript html css firebase vue.js


    【解决方案1】:

    我认为您实际上并没有在测验中存储任何内容。除其他问题外。但这里是你如何获取一个集合并存储它。

    <script>
    
      import firebase from 'firebase'
      export default {
        data: function() {
          return {
            quiz: [],
          }
        },
    
        firestore() {
          return {
            quiz: firebase.collection('Collection name here')
          }
        },
       
      }
    </script>
    

    如果您需要集合中的特定文档,那么

    <script>
    
      import firebase from 'firebase'
      export default {
        data: function() {
          return {
            quiz: [],
          }
        },
    
        firestore() {
          return {
            quiz: firebase.collection('Collection name here').doc('doc id here')
          }
        },
       
      }
    </script>
    

    【讨论】:

    • 其实测验就是一张地图
    • 我也在这样做:userResponses:Array(quiz.questions.length).fill(false),
    • 这给了我这个错误:53:27 错误“测验”未定义
    • 它更多的是你可以构建的东西。我使用 firestore 钩子发布的方式是查询 firestore 离开他们的文档的正确方法。存储在您想要的任何数据结构中。
    猜你喜欢
    • 2017-01-14
    • 2021-04-29
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 2022-09-12
    相关资源
    最近更新 更多