英文:
how to use onMounted to do async things in Nuxt 3
问题
I don't know what's wrong with this, the repository
ref's value just don't get updated
我不知道这有什么问题,repository
引用的值就是没有被更新
I wrote async/await, but it seems not working. repository
is an empty array.
我使用了 async/await,但似乎不起作用。repository
是一个空数组。
I can use the api out of onMounted
, so I think its something wrong about it. I don't know much about it.
我可以在 onMounted
外部使用 API,所以我认为问题可能出在这里。我对此不太了解。
Here's my code:
这是我的代码:
<template>
<div>
<h1>Starred Repositories</h1>
<ul>
<li v-for="repository in repositories" :key="repository.id">
<h3>{{ repository.name }}</h3>
<p>{{ repository.description }}</p>
</li>
</ul>
<button @click="nextPage" :disabled="isLastPage">Next Page</button>
</div>
</template>
<script setup>
import { Octokit } from '@octokit/core';
const repositories = ref([]);
const page = ref("1")
const runtimeConfig = useRuntimeConfig();
const octokit = new Octokit({
auth: runtimeConfig.githubToken,
});
const nextPage = () => {
page.value++;
}
onMounted(async () => {
const response = await octokit.request('GET /user/starred?page=' + page.value, {
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
});
repositories.value = response.data;
})
console.log(repositories.value);
</script>
also here's my app.vue:
还有我的 app.vue:
<script>
import Star from './components/Star.vue';
export default {
components: {
Star,
}
}
</script>
<template>
<Star />
</template>
Note: I've provided the translations for the code sections as requested.
英文:
I don't know what's wrong with this, the repository
ref's value just don't get updated
I wrote async/await, but it seems not working. repository
is an empty array.
I can use the api out of onMounted
, so I think its something wrong about it. I don't know much about it.
Here's my code:
<template>
<div>
<h1>Starred Repositories</h1>
<ul>
<li v-for="repository in repositories" :key="repository.id">
<h3>{{ repository.name }}</h3>
<p>{{ repository.description }}</p>
</li>
</ul>
<button @click="nextPage" :disabled="isLastPage">Next Page</button>
</div>
</template>
<script setup>
import { Octokit } from '@octokit/core';
const repositories = ref([]);
const page = ref("1")
const runtimeConfig = useRuntimeConfig();
const octokit = new Octokit({
auth: runtimeConfig.githubToken,
});
const nextPage = () => {
page.value++;
}
onMounted(async () => {
const response = await octokit.request('GET /user/starred?page=' + page.value, {
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
});
repositories.value = response.data;
})
console.log(repositories.value);
</script>
also here's my app.vue:
<script>
import Star from './components/Star.vue'
export default {
components: {
Star,
}
}
</script>
<template>
<Star />
</template>
答案1
得分: 0
以下是您要翻译的内容:
"Your console log will execute and log an empty array because it will be executed before the code in onMounted
, it is a hook and hooks don't themselves honour an async.
Move your console.log
so it is inside the onMounted
hook and also make sure the endpoint is infact retuning something other than an empty array it's self.
On a side note, using a life cycle hook for triggering API calls is not the best way, instead use the Top Level Await & Watch strategy.
https://nuxt.com/docs/getting-started/data-fetching#watch
In your case you are not using Nuxt's fetch helpers... so manual rigging looks like this:
<script setup>
import { Octokit } from '@octokit/core';
const repositories = ref([]);
const page = ref(1)
const runtimeConfig = useRuntimeConfig();
const octokit = new Octokit({
auth: runtimeConfig.githubToken,
});
const nextPage = () => {
page.value++;
}
const fetchStarredUsers = async (page) => {
const response = await octokit.request(`GET /user/starred?page=${page}`, {
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
});
if(Array.isArray(response?.data)) {
repositories.value = response.data
return
}
console.error('Invalid response, expected array of users').
}
// Call on entering component, (triggers loading suspense)
// https://nuxt.com/docs/getting-started/data-fetching#suspense
await fetchStarredUsers(page.value)
// Call on page being changed
watch(page, fetchStarredUsers)
</script>
英文:
Your console log will execute and log an empty array because it will be executed before the code in onMounted
, it is a hook and hooks don't themselves honour an async.
Move your console.log
so it is inside the onMounted
hook and also make sure the endpoint is infact retuning something other than an empty array it's self.
On a side note, using a life cycle hook for triggering API calls is not the best way, instead use the Top Level Await & Watch strategy.
https://nuxt.com/docs/getting-started/data-fetching#watch
In your case you are not using Nuxt's fetch helpers... so manual rigging looks like this:
<script setup>
import { Octokit } from '@octokit/core';
const repositories = ref([]);
const page = ref(1)
const runtimeConfig = useRuntimeConfig();
const octokit = new Octokit({
auth: runtimeConfig.githubToken,
});
const nextPage = () => {
page.value++;
}
const fetchStarredUsers = async (page) => {
const response = await octokit.request(`GET /user/starred?page=${page}`, {
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
});
if(Array.isArray(response?.data)) {
repositories.value = response.data
return
}
console.error('Invalid response, expected array of users').
}
// Call on entering component, (triggers loading suspense)
// https://nuxt.com/docs/getting-started/data-fetching#suspense
await fetchStarredUsers(page.value)
// Call on page being changed
watch(page, fetchStarredUsers)
</script>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论