简体   繁体   English

无法在函数外显示计算的返回值(Vue)

[英]Unable to display computed return value outside the function (Vue)

I've been working on a star rating system.我一直在研究星级评分系统。 I want to get the total rating value based on user ratings for each product separately and later get its average value.我想根据每个产品的用户评分分别获得总评分值,然后再获得其平均值。 I was able to get the logic right, however, I'm having trouble passing the total value from my computed property function (totalRating()) to get it displayed on my website.我能够获得正确的逻辑,但是,我无法从我的计算属性函数 (totalRating()) 传递总值以使其显示在我的网站上。 Right now I'm only focusing on returning my total value successfully.现在我只专注于成功返回我的总价值。

Here's the code:这是代码:

<template>
    <main>                       
       <div v-if="review.productId == $route.params.id">                  
           <div>Total: {{ totalRating }}</div> //fails to display the value               
           <div>({{ resultCount }} Review/s)</div>
       </div>           
    </main>

  
  </div>
</template>

<script>
import firebase from "firebase";
import "firebase/firestore";
import StarRating from "vue-star-rating";

export default {
  name: "ProductDetails",
  data() {
    return {
      itemId: this.$route.params.id,    
      items: [],
      item: {},
      reviews: [],
      review: {
        message: "",
        date: new Date().toLocaleString(),
        productId: this.$route.params.id,
        rating: 0,
        isRated: false,
        userId: "",
        username: "",
        email: "",
      },
    };
  },
  computed: {
    resultCount() {
      return this.filteredReviews.length;
    },
    totalRating() {          
      var totalValue = 0;
      firebase
        .firestore()
        .collection("reviews")
        .where("productId", "==", this.$route.params.id)
        .get(this.$route.params.id)

        .then(function(querySnapshot) {
          querySnapshot.forEach(function(total) {
            totalValue += total.data().rating;
          });
          console.log("in:" + totalValue); //success
        });
      console.log("out", totalValue); //fail
      return totalValue; 
    },
    filteredReviews: function() {
      return this.reviews.filter((review) => {
        return review.productId.match(this.$route.params.id);
      });
    },
  },
};
</script>

Can you try this你能试试这个吗

<template>
    <main>                       
       <div v-if="review.productId == $route.params.id">                  
           <div>Total: {{ totalRatingData }}</div> //fails to display the value               
           <div>({{ resultCount }} Review/s)</div>
       </div>           
    </main>

  
  </div>
</template>

<script>
import firebase from "firebase";
import "firebase/firestore";
import StarRating from "vue-star-rating";

export default {
  name: "ProductDetails",
  data() {
    return {
      itemId: this.$route.params.id,    
      items: [],
      totalRatingData: "",
      item: {},
      reviews: [],
      review: {
        message: "",
        date: new Date().toLocaleString(),
        productId: this.$route.params.id,
        rating: 0,
        isRated: false,
        userId: "",
        username: "",
        email: "",
      },
    };
  },
  computed: {
    resultCount() {
      return this.filteredReviews.length;
    },
    totalRating() {          
      var totalValue = 0;
      firebase
        .firestore()
        .collection("reviews")
        .where("productId", "==", this.$route.params.id)
        .get(this.$route.params.id)

        .then(function(querySnapshot) {
          querySnapshot.forEach(function(total) {
            totalValue += total.data().rating;
          });
           this.totalRatingData = totalValue;
          console.log("in:" + totalValue); //success
        });
      console.log("out", totalValue); //fail
      return totalValue; 
    },
    filteredReviews: function() {
      return this.reviews.filter((review) => {
        return review.productId.match(this.$route.params.id);
      });
    },
  },
};
</script>

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

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