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

Dom*_*Dom 5 vue.js vue-router vue-component vuejs3 vue-router4

我是 vue.js 初学者 (3)

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

  • 1 对于已连接的用户
  • 1 表示未连接的用户

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

 const routes = [
  {
    path: '/',
    name: 'Home',
    meta: { layout: 'layout-connected' },
    component: Home
  },
  {
    path: '/myspace',
    name: 'MySpace',
    meta: { auth: true },
    component: MySpace
  }
]
Run Code Online (Sandbox Code Playgroud)

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

<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)
    }
  },
Run Code Online (Sandbox Code Playgroud)

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

<template>
  <div class="container-fluid">
    <div class="row essai">
      <h1>layout non connected</h1>
      <slot />
    </div>
  </div>
</template>
Run Code Online (Sandbox Code Playgroud)

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

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

我已经很好地理解了这个概念吗?我的错误可能是什么?

谢谢

Bou*_*him 8

布局是应该使用以下方法在 main.js 中全局注册的组件:

app.component('layout-name',theLayoutComponent)
Run Code Online (Sandbox Code Playgroud)

或本地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)
    }
  },
Run Code Online (Sandbox Code Playgroud)