Search code examples
vue.jsnuxt.jsvue-i18nnuxt-i18n

How to get in script tag of vue file of / pages / when using Nuxt.js and nuxt-i18n


https://qiita.com/munieru_jp/items/d7e9f98b5ab5960e7a93

If you do as above, you can get the contents of {{$ t ('HELLO_WORLD')}} in the <template> tag. How can I get {{$ t ('HELLO_WORLD')}} in the <script> tag in the same file?

The reason is that I want to manage title descriptions and og related items with head.

We apologize for the inconvenience, but we would appreciate it if you could teach us. Then thank you.


Solution

  • You have access to the nuxt-i18n translation helper in the script of your page with this.$t and the head method on the other hand has access to the this context. So you could do something like this:

    export default {
        head () {
            return {
                title: this.$t('HELLO_WORLD')
            }
        }
    }