実現したいこと
Vue.jsの動的コンポーネントをちゃんと動くようにする。
現在のコード
現在は以下のコードの状態になっているのですが、動的コンポーネントとしてちゃんと切り替えることができていない点を解決したいです。
chromeのデベロッパーツールで見ると、
<button @click="toSecondComponent">SecondComponentにする</button>
の部分を押せばちゃんと<component :is="curerntComponent">
の部分が<secondcomponnet></secondcomponnet>
に変わってくれるのですが、SecondComponnet.vueの中に書いた<template><p>SecondComponentの中身です</p></template>
という文字列がページに出てきてくれません。
いろいろ調べても、どう修正したらいいのかわからなかったのでこちらで質問させていただきます。
vue.js
1<!--==================== template ====================--> 2<template> 3 <button @click="toFirstComponent">FirstComponentにする</button> 4 <button @click="toSecondComponent">SecondComponentにする</button> 5 6 <component :is="currentComponent" /> 7</template> 8 9<!--==================== script ====================--> 10<script setup> 11import { ref } from 'vue'; 12import FirstComponent from '../../components/FirstComponent.vue'; 13import SecondComponent from '../../components/SecondComponent.vue'; 14 15const currentComponent = ref('FirstComponent'); 16const toFirstComponent = () => { 17 currentComponent.value = 'FirstComponent'; 18}; 19const toSecondComponent = () => { 20 currentComponent.value = 'SecondComponent'; 21}; 22</script> 23 24<!--==================== style ====================--> 25<style scoped lang="scss"> 26 27</style>

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。