javascript - vue js挂载firebase异步调用不更新数据

标签 javascript firebase asynchronous vue.js

我知道问题是什么,但我无法在互联网上找到适合我的情况的解决方案。所以我用这一行从 firebase 调用数据:

this.$store.dispatch('getConsumptionFromFirebase')

但是在我从 firebase 获取数据之前,我在 Doughnut.vue 文件上的 Mounted() 函数被调用,因为当我转到其他组件并返回此处时,数据将被渲染,因为它之前已加载。我该如何解决这个问题,我需要立即渲染数据。这是代码:

我的 mainComponent.vue 文件:

<Doughnut class="chartSize" :labels="labelsDoughnut" :data="dataDoughnut" :colors="backgroundColorDoughnut"></Doughnut>


<script>
  import { mapGetters } from 'vuex'
  import Doughnut from '@/components/Graphs/Doughnuts'
  export default {
    components: {
      Doughnut
    },
    data () {
      return {
        labelsDoughnut: [ 'Air Conditioning & Heating', 'Cleaning Appliances' ],
        backgroundColorDoughnut: [ '#41B883', '#E46651' ]
      }
    },
    computed: {
      ...mapGetters({
        airConditioningHeatingMonthlyConsumption: 'airConditioningHeatingMonthlyConsumption',
        cleaningAppliancesMonthlyConsumption: 'cleaningAppliancesMonthlyConsumption'
      }),
      dataDoughnut: function () {
        return [ this.airConditioningHeatingMonthlyConsumption, this.cleaningAppliancesMonthlyConsumption ]
      }
    },
    created () {
      this.$store.dispatch('getConsumptionFromFirebase')
    }
  }
</script>

我的 Doughnut.vue 文件:

<script>
  import { Doughnut } from 'vue-chartjs'
  export default {
    props: ['labels', 'data', 'colors'],
    extends: Doughnut,
    data () {
      return {
        chartOptions: {
          legend: {
            position: 'top'
          }
        },
        dataCollection: {
          labels: this.labels,
          datasets: [ { data: this.data, backgroundColor: this.colors } ]
        } 
      }
    }, 
    mounted () {
      this.renderChart(this.dataCollection, this.chartOptions)
    }
  } 
</script>

最佳答案

我看到您通过调用 this.renderChart(this.dataCollection, this.chartOptions) 手动渲染组件

因此,使用计算代替数据也许是个好主意,然后观察:

<script>
  import { Doughnut } from 'vue-chartjs'
  export default {
    props: ['labels', 'data', 'colors'],
    extends: Doughnut,
    computed: {
        chartOptions () {
          return {
            legend: {
              position: 'top'
            }
          }
        },
        dataCollection () {
          return {
            labels: this.labels,
            datasets: [ { data: this.data, backgroundColor: this.colors } ]
          } 
        }
    },
    mounted () {
      this.renderChart(this.dataCollection, this.chartOptions)
    },
    watch: {
      chartOptions: function () {
        this.renderChart(this.dataCollection, this.chartOptions)
      },
      dataCollection: function () {
        this.renderChart(this.dataCollection, this.chartOptions)
      }
    }
  } 
</script>

关于javascript - vue js挂载firebase异步调用不更新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50569259/

相关文章:

android - 结构数据库 Firestore 帖子

javascript - 当已经有一个 window.load==function(){} 如何为 Canvas 添加 keyDown 事件

javascript - 检查数组是否存在电子邮件并使用 javascript 添加一个带有电子邮件和值的键?

android - 无法使用 android 模拟器上的 Firestore 模拟器访问云 Firestore 后端

ios - __block 变量返回 null。如何访问 __block 变量中的 NSString?

performance - 如何在 F# 中实现异步而不是并行

javascript - jQuery::Scrollable Div 不起作用

javascript - 检查变量是否未定义或不存在

swift - 如何在移动到下一行代码之前完成 NSURLSession 请求

c++ - 无限执行boost asio async_read_until