App.vue
CompOne.vue
CompTwo.vue
main.js
<template>
<h1>Dynamic Components</h1>
<p>Refresh the page and there is a chance the dynamic component will toggle.</p>
<component :is="Math.random() > 0.5 ? 'comp-one' : 'comp-two'"></component>
</template>
<style>
#app {
width: 350px;
margin: 10px;
}
#app > div {
border: solid black 2px;
padding: 10px;
margin-top: 10px;
}
</style>
<template>
<div>
<h2>One!</h2>
<p>This is component one.</p>
</div>
</template>
<script></script>
<style scoped>
div {
background-color: lightgreen;
}
</style>
<template>
<div>
<h2>Two!</h2>
<p>This is component two.</p>
</div>
</template>
<script></script>
<style scoped>
div {
background-color: lightpink;
}
</style>
import { createApp } from 'vue'
import App from './App.vue'
import CompOne from './components/CompOne.vue'
import CompTwo from './components/CompTwo.vue'
const app = createApp(App)
app.component('comp-one', CompOne)
app.component('comp-two', CompTwo)
app.mount('#app')