Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save NovoManu/9ccd6c1c6de3ff294106e024f1aeceed to your computer and use it in GitHub Desktop.
Save NovoManu/9ccd6c1c6de3ff294106e024f1aeceed to your computer and use it in GitHub Desktop.
<template>
<component :is="layout">
<slot />
</component>
</template>
<script>
import AppLayoutDefault from './AppLayoutDefault'
import { markRaw, watch } from 'vue'
import { useRoute } from 'vue-router'
export default {
name: 'AppLayout',
setup() {
const layout = markRaw(AppLayoutDefault)
const route = useRoute()
watch(
() => route.meta,
async meta => {
try {
const component = await import(`@/layouts/${meta.layout}.vue`)
layout.value = component?.default || AppLayoutDefault
} catch (e) {
layout.value = AppLayoutDefault
}
},
{ immediate: true }
)
return { layout }
}
}
</script>
@mcoredev
Copy link

I tried this code but don't worked me. Some changes i did and code is work.

	const layout = shallowRef({})
	layout.value = markRaw(AppLayoutDefault)

	watch( () => route.meta, async (meta) => {
		try {
			const component = await import(`@/layouts/${ meta.layout }.vue`)
			layout.value = component?.default || AppLayoutDefault
		} catch (e) {
			layout.value = AppLayoutDefault
		}
	},{ immediate: true })
	return { layout }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment