【发布时间】:2017-05-27 07:44:58
【问题描述】:
对于这样的组件
<template>
<div>
<router-link :to="{name:'section', params: { sectionId: firstSectionId }}">Start</router-link>
</div>
</template>
<script lang="ts">
import { mapActions } from "vuex"
export default {
mounted() {
this.getSectionId()
},
computed: {
firstSectionId() {
return this.$store.state.firstSectionId
}
},
methods: mapActions(["getSectionId"])
}
</script>
商店:
const store: any = new Vuex.Store({
state: {
firstSectionId: null
},
// actions,
// mutations
})
我在 getSectionId 操作中有一个 Web 请求,它异步获取数据并调用将填充 firstSectionId 到 state 的突变。在初始渲染期间,firstSectionId 是 null,我收到警告说在渲染router-link 期间缺少必需的参数。
这里添加v-if="firstSectionId"是没有问题的。但总的来说,从服务器获取数据以显示的方法是什么?目前我所有的组件都在渲染之前检查存储中是否存在数据,这是正常的还是有更好的方法在渲染之前等待数据加载?
【问题讨论】:
-
要在渲染之前等待数据加载,您使用服务器端渲染。除此之外,为什么要“等待数据”?我们每天都会看到网站和应用程序使用“加载”指示器加载静态页面结构,然后在它们到达时用数据填充它们。显然,尽可能早地向用户展示是最好的体验。