简体   繁体   English

Nuxt.js - 在页面中添加两个布局

[英]Nuxt.js - Add two layouts in page

I am a beginner in Nuxt.js and I am converting a project of Vue to Nuxt.js and I wanted to use two layouts (the default one and another) on one page.我是Nuxt.js的初学者,我正在将 Vue 项目转换为 Nuxt.js,我想在一页上使用两种布局(默认布局和另一种布局)。 The logic is this: The first layout is the (default) or header which is on all pages and the second layout is the settings bar .逻辑是这样的:第一个布局是(默认)或 header,它在所有页面上,第二个布局是设置栏
In settings page i have 3 routes (see project structure here: image ):设置页面中,我有 3 条路线(请参阅此处的项目结构:图片):

  1. settings/avatar设置/头像
  2. settings/account设置/帐户
  3. settings/about设置/关于

I want the Settings bar to be the same for three routes.我希望三个路线的设置栏相同。 I can add the Settings bar to all three child pages like: layout: 'settings-bar' but then could not set the Header layout.我可以将设置栏添加到所有三个子页面,例如: layout: 'settings-bar' ,但随后无法设置 Header 布局。 In my Vue project i only used in settings page: Settings bar component and below <router-view></router-view> to change the components.在我的 Vue 项目中,我只在设置页面中使用:设置栏组件和下面<router-view></router-view>来更改组件。 Any idea how i can do this?知道我该怎么做吗? in docs can not find anything.在文档中找不到任何东西。 See other screenshots here to better understand:在此处查看其他屏幕截图以更好地理解:

项目

There is no way to have multiple layouts on one page.没有办法在一页上有多个布局。 However what you are describing could be managed with Child Components .但是,您所描述的内容可以使用Child Components进行管理。

It is actually doable to have nested layouts in Nuxt, meanwhile: it's a bit hacky and hard to read and I'm not sure that I may recommend it at a bigger scale.同时,在 Nuxt 中拥有嵌套布局实际上是可行的:它有点 hacky 且难以阅读,我不确定我是否会在更大范围内推荐它。 Tried it, do not recommend but if it's really needed, here is the solution.试过了,不推荐,但如果真的需要,这里是解决方案。

layouts/default.vue

<template>
  <div>
    <nuxt v-if="!$slots.default" />
    <slot />
  </div>
</template>

layouts/newLayout.vue

<template>
  <default-layout>
    <h1>Surrounding layout</h1>
    <nuxt />
  </default-layout>
</template>

<script>
import DefaultLayout from '~/layouts/default.vue';

export default {
  components: {
    DefaultLayout
  }
}
</script>

Then, you can use it anywhere with然后,您可以在任何地方使用它

<script>
export default {
  layout: 'newLayout' // name of your new layout
}
</script>

Kudos to this article: https://constantsolutions.dk/2020/02/nested-layouts-in-nuxt-vue-js/感谢这篇文章: https://constantsolutions.dk/2020/02/nested-layouts-in-nuxt-vue-js/

Not sure, but the article itself may be from this github post: https://github.com/nuxt/nuxt.js/issues/785#issuecomment-422365721不确定,但文章本身可能来自此 github 帖子: https://github.com/nuxt/nuxt.js/issues/785#issuecomment-422365721

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

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