简体   繁体   English

Nuxt - 在运行时处理路由并激活 ssr

[英]Nuxt - Handle routes at runtime with ssr activated

Hello there: quick question I've你好:我有一个快速的问题

  • a Nuxt app with the target: 'server' in the nuxt.config.js具有target: 'server'
  • an API with an endpoint that give me, for a given path, a template associated ( eg: /person/maxime will give me template: PersonsSingle )一个带有端点的 API 给我,对于给定的路径,关联的模板(例如: /person/maxime maxime 会给我模板: PersonsSingle
  • a vue component that handles this page ( eg: _pages/person.vue )处理此页面的 vue 组件(例如: _pages/person.vue

How can I handle this at runtime?我该如何在运行时处理这个? I guess it's something to do with the middlewares but which one and what is the best strategy here?我想这与中间件有关,但这里的最佳策略是哪一个?

PS: I use this system because two urls with the same structure (eg: /person/maxime and /person/sam could lead to two completely different templates ). PS:我使用这个系统是因为两个具有相同结构的 url(例如: /person/maxime/person/sam可能会导致两个完全不同的模板)。 Don't ask why I have to deal with it:/不要问我为什么要处理它:/

This is how I would do it.我就是这样做的。

async asyncData({ $axios }) {
  const data = await $axios.$get('...')

  const templateType = data.templateType

  return {
    templateType
  }
}
<template>
  <div>
    <page-1 v-if="templateType === 'foo'" />

    <page-2 v-else />
  </div>
</template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM