【发布时间】:2019-08-05 05:35:25
【问题描述】:
在 Nuxt.js 项目中,我有一个页面,我需要在其中进行一些客户端计算,然后才能在表格中显示一些数据。 在计算之前和计算期间,我想显示一个加载屏幕。
一些背景:数据是农民使用的地块。该表应该显示过去几年在一块地块上种植了哪种作物。 数据在数据库中的存储方式如下:
plots = [{
name: 'Plot1',
year: 2017,
crop: 'Wheat'
...
}, {
name: 'Plot1',
year: 2018,
crop: 'Maize',
...
} ...]
在将数据转换为以下结构的嵌套对象的方法中
data = {
'Plot1': {
2017: {
'crop': 'Wheat',
'catchCrop': true
},
2018: {
'crop': 'Maize',
'catchCrop': false
}
}
...
}
并随后显示在表格组件中。
组件的模型如下所示:
<template>
<loadingComponent v-if="loading"/>
<tableComponent v-else-if="!loading && dataAvailable"/>
<span v-else >No data</span>
</template>
<script>
data() {
return {
loading: true,
dataAvailable: false
}
},
mounted() {
this.startCalculation()
},
methods: {
startCalculation() {
if (store.data) {
// long running calculation, then
this.dataAvailable = true
}
this.loading = false
}
}
</script>
我面临的问题是加载组件永远不会显示。
但是,startCalculation 方法会阻塞用户界面(如果显示加载组件就可以了),并且该组件仅在计算完成后更新。
有人知道我该如何规避这个问题吗? 提前非常感谢!
编辑:
在摆弄之后,我可以通过将setTimeout 设置为 1ms 来让它按照我想要的方式工作。这样,加载指示器显示出来,数据被正确处理,计算完成后加载指示器被成功移除。然而,这感觉就像一个非常肮脏的黑客,我很想避免它......
mounted() {
this.loading = true
// set short timeout in order for Vue to render the loading bar
setTimeout(() => {
this.startCalculation()
this.loading = false
},1)
}
【问题讨论】:
标签: vuejs2 vue-component nuxt.js