Axios + VueJs3: 使用异步导出显示结果

huangapple go评论69阅读模式
英文:

Axios + VueJs3: Display the Results using Async Export

问题

以下是您要翻译的代码部分:

我正在使用vue.js3与vite和axios

我定义了一个带有此函数的TS文件

export async function findAllCompanies() {
    try {
        const {data, status} = await axios.get<CompanyPojoResponse>(
            'http://localhost:9050/api/v1/company/all',
            {
                headers: {
                    'Content-Type': 'application/json',
                    Authorization: 'Basic YWRtaW46YWRtaW5QYXNz',
                },
            },
        );

        console.log(JSON.stringify(data, null, 4));

        // ?? "response status is: 200"
        console.log('response status is: ', status);

        return data;
    } catch (error) {
        if (axios.isAxiosError(error)) {
            console.log('error message: ', error.message);
            console.log('error message: ', error);
            return error.message;
        } else {
            console.log('unexpected error: ', error);
            return 'An unexpected error occurred';
        }
    }
}

然后我尝试在一个vue文件中显示它

<script setup lang="ts">

import type {CompanyPojo} from "@/rest/restapi-companies";
import {findAllCompanies} from "@/rest/restapi-companies";

let companyResult = findAllCompanies()
console.log(companyResult)

</script>

<template>

  <div class="top-nav">
    <nav>
      <RouterLink to="/new-company">New Company</RouterLink>
      <!-- view product list -->
      <!-- create new product -->
    </nav>
  </div>

  <table id="companyTable">
    <thead>
    <tr>
      <th>JUST EVERYTHING....</th>
    </tr>
    </thead>
    <tbody>

    <tr v-for="company in companyResult">
      <td>{{ company }}</td>
    </tr>
    </tbody>
  </table>


</template>

请注意,上述代码包括Vue.js、Vite和Axios的相关内容。如果您需要关于特定部分的帮助或翻译,请提供更具体的信息。

英文:

I am using vue.js3 with vite and axios.

i defined a TS file with this function:

export async function findAllCompanies() {
try {
const {data, status} = await axios.get&lt;CompanyPojoResponse&gt;(
&#39;http://localhost:9050/api/v1/company/all&#39;,
{
headers: {
&#39;Content-Type&#39;: &#39;application/json&#39;,
Authorization: &#39;Basic YWRtaW46YWRtaW5QYXNz&#39;,
},
},
);
console.log(JSON.stringify(data, null, 4));
// ?? &quot;response status is: 200&quot;
console.log(&#39;response status is: &#39;, status);
return data;
} catch (error) {
if (axios.isAxiosError(error)) {
console.log(&#39;error message: &#39;, error.message);
console.log(&#39;error message: &#39;, error);
return error.message;
} else {
console.log(&#39;unexpected error: &#39;, error);
return &#39;An unexpected error occurred&#39;;
}
}
}

then, i am trying to display it in a vue-file:

&lt;script setup lang=&quot;ts&quot;&gt;
import type {CompanyPojo} from &quot;@/rest/restapi-companies&quot;;
import {findAllCompanies} from &quot;@/rest/restapi-companies&quot;;
let companyResult = findAllCompanies()
console.log(companyResult)
&lt;/script&gt;
&lt;template&gt;
&lt;div class=&quot;top-nav&quot;&gt;
&lt;nav&gt;
&lt;RouterLink to=&quot;/new-company&quot;&gt;New Company&lt;/RouterLink&gt;
&lt;!-- view product list --&gt;
&lt;!-- create new product --&gt;
&lt;/nav&gt;
&lt;/div&gt;
&lt;table id=&quot;companyTable&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;JUST EVERYTHING....&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr v-for=&quot;company in companyResult&quot;&gt;
&lt;td&gt;{{ company }}&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/template&gt;

I see results within the logs, but not at the page (added company just for testing)
trying to access fields:
<td>{{ company.name }}</td>
<td>{{ company.additionalName }}</td>

is just resulting in an error, that CompanyPojoResponse does not have those fields.

which i really don't understand, since:

export type CompanyPojoResponse = {
data: CompanyPojo[];
};

I thought i am looping over the data at this point and then i loop therefor over CompanyPojo-Objects...

答案1

得分: 1

findAllCompanies() 是一个异步函数,你没有等待承诺被解决。

你可以像这样做:

const companyResult = ref([]) //将其初始化为空数组

onMounted(async () => {
  companyResult.value = await findAllCompanies()
})

英文:

Your findAllCompanies() is an async function, you are not waiting for the promise to be resolved.

You can do something like this:

const companyResult = ref([]) // Initialize this as an empty array

onMounted(async () =&gt; {
  companyResult.value = await findAllCompanies()
})

huangapple
  • 本文由 发表于 2023年3月7日 06:59:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75656599.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定