Skip to content

Exposing methods for Script Setup 'SFC Composition API Syntax Sugar' #138

Open
@ux-engineer

Description

@ux-engineer

Vue 3 has this yet experimental syntax Script Setup 'SFC Composition API Syntax Sugar'.

I'm wondering how to best expose the translation method t in components written with this syntax.

Currently this syntax is working for me:

<script setup lang="ts">
import { useI18n } from 'vue-i18n';

const { t } = useI18n();

export const name = 'ViewName';

// Note: vue-meta package does not support Vue 3 as of yet, but am verifying functionality
// by being able to use this object in template syntax.
export const metaInfo = {
  title: t('PAGE_TITLE'),
  meta: [
    { name: 'description', content: t('PAGE_DESCRIPTION') },
  ],
};
</script>

<template>
  <h1>{{ metaInfo.title }}</h1>
</template>

However, I would like to be able to write it like this:

<script setup lang="ts">
export const name = 'ViewName';

export const metaInfo = {
  title: $t('PAGE_TITLE'),
  meta: [
    { name: 'description', content: $t('PAGE_DESCRIPTION') },
  ],
};
</script>

Would it be possible for this library to automatically expose $t method in script setup scope? Or by object-destructuring from setup context <script setup="_, { $t }" lang="ts">?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions