如何在setup的h渲染函数中使用component标签来实现动态组件渲染 #6425
Unanswered
yn36
asked this question in
Help/Questions
Replies: 3 comments 2 replies
-
This is the Docs, Hope this can help you. <script>
import { ref, h, resolveComponent } from 'vue'
import Comp from './Comp.vue'
import Comp1 from './Comp1.vue'
export default {
setup() {
const random = ref(0)
function change() {
const r = Math.random()
random.value = r
}
return () => h('div',
[h('button', {onClick: change}, 'change'),
h('br'),
random.value > 0.5 ? h(Comp, null) : h(Comp1, null),
h('div', {}, random.value)
]
)
},
components: {
Comp,
Comp1
}
}
</script> |
Beta Was this translation helpful? Give feedback.
2 replies
-
The code with <script setup>
import { ref } from 'vue'
import Comp from './Comp.vue'
</script>
<template>
<component :is='Comp' />
</template> are compiler to /* Analyzed bindings: {
"ref": "setup-const",
"Comp": "setup-const"
} */
import { resolveDynamicComponent as _resolveDynamicComponent, openBlock as _openBlock, createBlock as _createBlock } from "vue"
import { ref } from 'vue'
import Comp from './Comp.vue'
const __sfc__ = {
__name: 'App',
setup(__props) {
return (_ctx, _cache) => {
return (_openBlock(), _createBlock(_resolveDynamicComponent(Comp)))
}
}
}
__sfc__.__file = "App.vue"
export default __sfc__ |
Beta Was this translation helpful? Give feedback.
0 replies
-
<template>
<button v-for="tab in tabs" :key="tab" @click="currentTab = tab.component">
{{ tab.name }}
</button>
<component :is="currentTab ? currentTab : defaultTab"></component>
</template>
<script setup>
import { markRaw, ref } from "vue";
import tabOne from "./TabOne.vue";
import tabTwo from "./TabTwo.vue";
const tabs = ref([
{ name: "Tab 1", component: markRaw(tabOne) },
{ name: "Tab 2", component: markRaw(tabTwo) },
]);
const currentTab = ref(null);
const defaultTab = tabOne;
</script> |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Beta Was this translation helpful? Give feedback.
All reactions