问答

vue <template></template>初次渲染时需要

作者:admin 2021-09-06 我要评论

template {{handleMachineRoom(scope.row.idcCode)}}/templatescriptcomputed: { machineRoom(){ return this.$store.state.machineRoom; } }, mounted() { if(t...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)
<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处理结果为空,等机房列表获取时已经晚了,请问这个情况应该怎么处理?

###

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

###

handleMachineRoom拿到computed里面

###

scope.row.idcCode 作为计算属性,当异步返回值后,会自动变化

###

初始的时候隐藏,有数据再显示,用 v-if 判断一下。

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • vue &lt;template&gt;&lt;

    vue &lt;template&gt;&lt;

  • 关于js用function和用class定义的类中

    关于js用function和用class定义的类中

  • iview中的modal组件怎么设置高度?

    iview中的modal组件怎么设置高度?

  • Nginx和前端缓存问题

    Nginx和前端缓存问题

腾讯云代理商
海外云服务器