Heim  >  Fragen und Antworten  >  Hauptteil

Vue 3 vue-i18n. Wie verwende ich $t(t) außerhalb der Anwendung (.vue-Datei)? Synthese-API

<p>Ich möchte meine Datums- und Uhrzeitangaben außerhalb der Komponente formatieren. </p> <pre class="brush:js;toolbar:false;">function transformDatetime(config, value) { if (Wert) { return $d(new Date(value), config); } return $t('-'); } </pre> <p>Ich versuche, $t von der Anwendungsinstanz abzurufen. Es funktioniert aber nur im Komponentenkontext, genau wie useI18n. </p> <pre class="brush:js;toolbar:false;">import { getCurrentInstance } from 'vue' Standardfunktion exportieren useGlobal() { const-Instanz = getCurrentInstance() if (!instance) return Rückkehrinstanz.appContext.config.globalProperties } </pre> <p><br /></p>
P粉032649413P粉032649413419 Tage vor418

Antworte allen(1)Ich werde antworten

  • P粉143640496

    P粉1436404962023-08-27 00:25:26

    我找到了解决方案。只需将您的 i18n 导入到应用程序外部的文件中,然后使用 i18n.global.t

    import { createI18n } from "vue-i18n"
    
    export const i18n = createI18n({
        legacy: false,
        locale: 'ja',
        fallbackLocale: 'en',
        messages,
    })
    
    import { i18n } from '@/i18n'
    
    const { t: $t, d: $d, n: $n } = i18n.global
    
    const expample = $t('some-text')
    

    Antwort
    0
  • StornierenAntwort