如何在 Nuxt.js 中处理子页面的多个布局?

Aks*_*ngh 2 vue.js nuxt.js

我是第一次使用Nuxt.js和。Vue.js所以,我想使用一个模板作为父模板,它基本上需要有两列。每个孩子的两列都有不同的数据。

layouts/content.vue:

<template>
    <b-container fluid>
        <b-row>
            <!--<b-col><nuxt/></b-col>
            <b-col><nuxt/></b-col>-->
        </b-row>
    </b-container>
</template>
Run Code Online (Sandbox Code Playgroud)

这是子 vue 组件:

pages/some-page/index.vue:

<template>
    <div>
        <!-- how to enter data for both the columns -->
    </div>
</template>

<script>
    export default {
        layout: 'content'
    }
</script>
Run Code Online (Sandbox Code Playgroud)

我如何输入处理我的两列content.vue?我想在某些父布局中创建两列,以便以后编辑时,我只有一个文件,其中的更改会反映到所有子页面。

Man*_*piK 6

你不能将数据传递到布局(你可以使用 vuex),并且你不能使用两次<nuxt />。您应该使用组件插槽:

layouts/content.vue:

<template>
    <b-container fluid>
        <nuxt />
    </b-container>
</template>
Run Code Online (Sandbox Code Playgroud)

components/two-cols.vue:

<template>
    <b-row>
        <b-col><slot name="col-1"></slot></b-col>
        <b-col><slot name="col-2"></slot></b-col>
    </b-row>
</template>
Run Code Online (Sandbox Code Playgroud)

pages/some-page/index.vue:

<template>
    <two-cols>
        <template slot="col-1>
            <!-- col-1 html here -->
        </template>
        <template slot="col-2>
            <!-- col-2 html here -->
        </template>
    </two-cols>
</template>

<script>
    import TwoCols from '~/components/two-cols.vue`
    export default {
        layout: 'content',
        components: {
            TwoCols
        }
    }
</script>
Run Code Online (Sandbox Code Playgroud)

  • 刚刚在 github 上找到了 @atinux 的回复:https://github.com/nuxt/nuxt.js/issues/190#issuecomment-277304451 (2认同)