基于请求的其他应用的动态路由

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

Dynamic routes based on request to other app

问题

I'm currently creating an app in nuxt 3. This app has only one task:

  1. Get and parse request url
  2. pass request url params to other app via API
  3. 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:

  1. Get and parse request url
  2. pass request url params to other app via API
  3. 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

&lt;template&gt;
  &lt;NuxtPage :name=&quot;data.page&quot;&gt;
&lt;template&gt;

&lt;script setup&gt;
// here some shortened logic
const route = useRoute().fullPath;
const data = new RequestToBe(route).send();

console.log(data) // { page: &#39;post&#39;, ... }
&lt;/script&gt;

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:

&lt;template&gt;
  &lt;NuxtPage /&gt;
&lt;/template&gt;

&lt;script setup&gt;
// here some shortened logic

const router = useRouter()
const route = useRoute().fullPath;

try {
  const data = new RequestToBe(route).send();
  console.log(data) // { page: &#39;post&#39;, ... }
  if (data.page === &#39;post&#39;) {
    // go to post page
    router.push(&#39;/post&#39;)
  } else {
    // or do something else
  }
} catch (error) {
  // something went wrong
  console.log(error)
}
&lt;/script&gt;

</details>



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

发表评论

匿名网友

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

确定