Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
587 views
in Technique[技术] by (71.8m points)

vue <template></template>初次渲染时需要用到异步数据应该怎么处理?

<template>
    {{handleMachineRoom(scope.row.idcCode)}}
</template>

<script>
computed: {
      machineRoom(){
        return this.$store.state.machineRoom;
      }
    },
    
mounted() {
    if(this.machineRoom.length > 0)return;
    this.$store.dispatch('onGetMachineRoom')
}
methods: {
    handleMachineRoom(i){
        let f = {};
        if(this.machineRoom.length > 0){
          f = this.machineRoom.find(item => {
            return item.idc_code === i;
          })
        }
        return f.idc_name?f.idc_name:'';
      },

}
</script>

如上图,初次渲染时有机房代码,需要在机房列表中匹配对应的中文名称,但是机房列表可能为空,这时会去调用action中的方法获取,但是这是异步的过程,当渲染完成时,handleMachineRoom处理结果为空,等机房列表获取时已经晚了,请问这个情况应该怎么处理?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

vue的几个生命周期函数应该可以帮到你点击查看vue生命周期


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...