英文:
Dynamic routes based on request to other app
问题
I'm currently creating an app in nuxt 3. This app has only one task:
- Get and parse request url
- pass request url params to other app via API
- render one of pages conditionally, based on response
In my project I've got:
[...]
/pages
- post.vue
- user.vue
- info.vue
- [...]
app.vue
[...]
In app.vue
<template>
<NuxtPage :name="data.page">
<template>
<script setup>
// here some shortened logic
const route = useRoute().fullPath;
const data = new RequestToBe(route).send();
console.log(data) // { page: 'post', ... }
</script>
How can I render my pages, based on data? Should I config router, or solve it by rendering logic?
英文:
I'm currently creating an app in nuxt 3. This app has only one task:
- Get and parse request url
- pass request url params to other app via API
- render one of pages conditionally, based on response
In my project I've got:
[...]
/pages
- post.vue
- user.vue
- info.vue
- [...]
app.vue
[...]
In app.vue
<template>
<NuxtPage :name="data.page">
<template>
<script setup>
// here some shortened logic
const route = useRoute().fullPath;
const data = new RequestToBe(route).send();
console.log(data) // { page: 'post', ... }
</script>
How can I render my pages, based on data? Should I config router, or solve it by rendering logic?
答案1
得分: 1
以下是您要的代码的翻译部分:
<template>
<NuxtPage />
</template>
<script setup>
// 这里是一些缩短的逻辑
const router = useRouter()
const route = useRoute().fullPath;
try {
const data = new RequestToBe(route).send();
console.log(data) // { page: 'post', ... }
if (data.page === 'post') {
// 转到文章页面
router.push('/post')
} else {
// 或者执行其他操作
}
} catch (error) {
// 出现错误
console.log(error)
}
</script>
英文:
What doesn’t work in your setup? Imagine you can check data.page before changing route:
<template>
<NuxtPage />
</template>
<script setup>
// here some shortened logic
const router = useRouter()
const route = useRoute().fullPath;
try {
const data = new RequestToBe(route).send();
console.log(data) // { page: 'post', ... }
if (data.page === 'post') {
// go to post page
router.push('/post')
} else {
// or do something else
}
} catch (error) {
// something went wrong
console.log(error)
}
</script>
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论