S
S
stanislavkm2022-04-20 22:34:35
Vue.js
stanislavkm, 2022-04-20 22:34:35

How to implement the correct work of the dynamic layout component in vue3?

Where is the mistake?

app.vue

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

<script>
export default {
  name: 'App',
  computed: {
    layout() {
      const layoutName = this.$route.meta.layout || 'DefaultLayout';
      return () => import(`@/layouts/${layoutName}.vue`)
    }
  },
}
</script>


index.js (router)
...
import Home from "../views/Home";
const routes = [
    {
        path: '',
        name: 'Home',
        component: Home,
        meta: {
            layout: () => import('../layouts/DefaultLayout')
        }
    },
...


Well, actually there are DefaultLayout and Home components.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Anton, 2022-04-20
@Fragster

Don't reinvent the wheel, use nested routes https://router.vuejs.org/guide/essentials/nested-r...

A
Alexander Medvedev, 2022-04-22
@lifestar

There is a handy thing for Vite - vite-plugin-vue-layouts

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question