1

I want to implement simple tabs using script setup and composition API

<script setup>
import Timeline from './Timeline.vue';
import Profile from './Profile.vue';
import Groups from './Groups.vue';

const currentTab = ref('Timeline')
const tabs = ref(['Timeline', 'Profile', 'Groups'])
</script>

<template>
    <div class="tabs">
       <div v-for="tab in tabs"
       :key="tab"
       @click="currentTab = tab" v-text="tab"

       <component :is="currentTab"></component>
    </div>
</template>

But this code will only result <timeline></timeline> instead of the actual content of the Timeline component.

0

1 Answer 1

2

As mentioned here when using the <script setup>-method you need to pass the actual object of the component to the :is instead of the string.

See this Example here

Here is also the code itself:

<script setup>
import Timeline from './Timeline.vue';
import Profile from './Profile.vue';
import Groups from './Groups.vue';
import { shallowRef } from 'vue';

const currentTab = shallowRef(Timeline);
const tabs = [Timeline, Profile, Groups];
</script>

<template>
    <div class="tabs">
       <div v-for="tab in tabs"
        :key="tab"
        @click="currentTab = tab" v-text="tab.__name">
      </div>

    </div>
  <keep-alive>
    <component :is="currentTab"></component>
  </keep-alive>
</template>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.