VueJs-How to complete an asynchronous request before rendering html?

217 views Asked by At

I use el-popover try to show something, here is the code:

<template v-slot:userItem="testData" >
    <template v-if="testData.roleData!=null">
      <el-popover
      v-for ="(item,index) in testData.roleData.split(',')"
      :key="index"
      placement="top-start"
      title="Authority"
      width="200"
      trigger="hover"
      :content="getClaimValue(item.split(':')[0])">
       <el-link  slot="reference" type="primary"  @click="test1(item.split(':')[0])" >{{item.split(':')[1]}}</el-link>
       </el-popover>
    </template>

Now the question is that I cannot see the content of the el-popover. I guess it is because the method getClaimValue() is asynchronous, here is the code:

private getClaimValue(roleId:any){
    axios.get(`${env.identityServer}/User/GetClaimByRoleId?roleId=${roleId}`)
                .then((response) => {
                    console.log(response.data);
                    return this.updateTransfer(response.data);
                    
                })
                .catch(
                  (error) => {   
                         Message({
                            message: error.message,
                            type: 'error',
                            duration: 3 * 1000
                          })                  
                  });
  }

So what's the problem and how could I see my content of el-popover ? Thank you very much.

1

There are 1 answers

0
Qiu Zhou On BEST ANSWER

Instead of trying to get response from axios request directly by return (which I think you can't), you can save response data into another object, for example, contents, when the request is finished, the el-popover content will be updated automatically.

in your xxx.vue:

 export default class Test extends Vue {

       contents: Object = {};// <--- where to store all contents for el-popovers

       private getClaimValue(roleId:any){
           if(!this.contents[roleId]){
             this.contents[roleId] = "loading";
             axios.get(`${env.identityServer}/User/GetClaimByRoleId?roleId=${roleId}`)
                .then((response) => {
                    console.log(response.data);
                    this.contents[roleId] = this.updateTransfer(response.data);
                    
                })
                .catch(
                  (error) => {   
                         Message({
                            message: error.message,
                            type: 'error',
                            duration: 3 * 1000
                          })                  
                  });
             }
           }
          
           return this.contents[roleId];
        }

      
 }