5

It seems that it is impossible to convert the Options API code below to <script setup>:

    <template>
      <div>
        <component :is="layout">
          <router-view/>
        </component>
      </div>
    </template>
    
    <script>
    const defaultLayout = 'default'
    
    import Dark from './layouts/dark.vue'
    import Light from './layouts/light.vue'
    import Default from './layouts/default.vue'
    
    export default {
      name: 'App',
    
      components: {
        Dark,
        Light,
        Default
      },
    
      computed: {
        layout () {
          return (this.$route.meta.layout || defaultLayout)
        }
      }
    }
    </script>

My attempt:

    <script setup>
    import { computed } from 'vue'
    import { useRouter, useRoute } from 'vue-router'
    
    import Dark from './layouts/dark.vue'
    import Light from './layouts/light.vue'
    import Default from './layouts/default.vue'
    
    const router = useRouter()
    const route = useRoute()
    
    const defaultLayout = 'default'
    const layout = computed(() => route.meta.layout || defaultLayout )
    </script>

Result:

No layout is loaded with the <script setup> option but just a <default>, <dark>, or <light> block, for example:

    <div id="app">
    <default>
    ...
    ...
    </default>
    </div>

According to the doc here, we can use a ternary:

    <component :is="someCondition ? Foo : Bar" />

But that is not ideal. As you might have more than 2 dynamic components to work out. We can't make the ternary too long. Can we?

Any ideas?

1
  • 2
    I got a similar question about to use component :is="" with script setup. Hope it will help you Commented Sep 18, 2021 at 20:36

1 Answer 1

14

The trick is to return the referenced variable rather than it's name.

    <script setup>
    import { computed } from 'vue'
    import { useRouter, useRoute } from 'vue-router'
    
    import Dark from './layouts/dark.vue'
    import Light from './layouts/light.vue'
    import Default from './layouts/default.vue'
    
    const router = useRouter()
    const route = useRoute()
    
    const layouts = {
      default: Default,
      light: Light,
      dark: Dark
    };
    
    const defaultLayout = 'default'
    const layout = computed(() => layouts[route.meta.layout || defaultLayout] )
    </script>

Reason

While with options API components are registered locally and can be resolved by it's name (string key), with <script setup> the components are used directly without registration so it is not possible to use string key to resolve them...

Sign up to request clarification or add additional context in comments.

1 Comment

As per the vue's documentation, you can use the name string of a component and vue will resolve that component as long as it is registered in that component. And as per vue's documentation a component is registered when importing that component when using script setup. No mention of not being able to use the string name in script setup.

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.