Nested components in template generated by PHP. #14204
Replies: 2 comments
|
I think the timing is the main issue here. By the time So I don't think calling: instance.appContext.app.component("othercomponent", othercomponent)inside Out of curiosity, what problem are you ultimately trying to solve? If the PHP template is dynamic and you don't know all of the possible components ahead of time, there may be a different approach (such as registering them globally before |
|
The existing answer is exactly right about the timing. Here's the complete working solution for the dynamic-PHP-template use case. Why registering inside The fix that actually works for your case — use <!-- PHP-rendered -->
<div id="vueApp">
<h1>Im header</h1>
<testcomponent>
<othercomponent />
</testcomponent>
</div>// app.ts
import { createApp } from 'vue'
import testComponent from './components/testComponent.vue'
import otherComponent from './components/otherComponent.vue'
const app = createApp({})
app.component('testcomponent', testComponent)
app.component('othercomponent', otherComponent)
app.mount('#vueApp')Register everything before // For truly dynamic component sets:
const available = await fetch('/api/available-components').then(r => r.json())
for (const { name, loader } of available) {
app.component(name, (await loader()).default)
}
app.mount('#vueApp')Alternative — keep dynamic registration but route through <!-- DynamicShell.vue -->
<script setup lang="ts">
import { computed } from 'vue'
const props = defineProps<{
component: string // e.g. 'othercomponent'
}>()
// Single source of truth — register them all up-front in app.ts
const registry = useComponents() // your own composable returning a Map<string, Component>
const Resolved = computed(() => registry.value.get(props.component))
</script>
<template>
<component :is="Resolved" />
</template>Now PHP emits: <div id="vueApp">
<DynamicShell component="testcomponent">
<DynamicShell component="othercomponent" />
</DynamicShell>
</div>Every nested layer is resolved at runtime through the registry. Adding a new component becomes a server-side change plus a registry entry — no rebuild, no setup() timing issue. One thing to avoid: don't reach for Note on bundle size: you're using |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi!
I have template rendered by php:
With that Vue code all working fine:
But when I try register component inside other component its not work ...
app.js
./components/testComponent.vue
Im used inline-template in vue2 ... but how to make in vue3 I dont understand.
Im understand, slot is compiled. Maybe is it possible recompile after component registration or has other ways?
Help me please!
All reactions