Vue.js 3 - 尝试构建具有 2 种布局的系统

Vue.js 3 - Trying to build a system with 2 layouts

我是初学者 vue.js (3)

我尝试构建一个具有 2 种布局的系统:

在我的 router/index.js 中,我为每个路由添加了一个元数据:

 const routes = [
  {
    path: '/',
    name: 'Home',
    meta: { layout: 'layout-connected' },
    component: Home
  },
  {
    path: '/myspace',
    name: 'MySpace',
    meta: { auth: true },
    component: MySpace
  }
]

在我的 App.vue 中,我决定使用哪种布局 (参见“:is="布局”):

<template>
  <div id="app">
    <component :is="layout">
      <router-view/>
    </component>
  </div>
</template>

<script>
const defaultLayout = 'layout-not-connected'

export default {
  computed: {
    layout () {
      console.log(this.$route.meta.layout)
      return (this.$route.meta.layout || defaultLayout)
    }
  },

至少,在我的布局中我有:

<template>
  <div class="container-fluid">
    <div class="row essai">
      <h1>layout non connected</h1>
      <slot />
    </div>
  </div>
</template>

当我console.log应用哪个路由时,它工作正常:我在控制台中有正确的布局。

但我从来没有看到布局(例如标签)。只有组件。

我对这个概念理解得很好吗?我的错误是什么?

谢谢

布局是应该在 main.js 中全局注册的组件,使用:

app.component('layout-name',theLayoutComponent)

或本地 components 选项:

<template>
  <div id="app">
    <component :is="layout">
      <router-view/>
    </component>
  </div>
</template>

<script>
const defaultLayout = 'layout-not-connected'
import LayoutConnected from 'path/to/LayoutConnectedComponent'
import DefaultLayout from 'path/to/DefaultLayout Component'
export default {
 components:{
DefaultLayout,LayoutConnected
 },
  computed: {
    layout () {
      console.log(this.$route.meta.layout)
      return (this.$route.meta.layout || defaultLayout)
    }
  },