javascript - vue.js:597 [Vue warn]: Property or method "$t" is not defined - Stack Overflow

I am trying to implement vue-i18n Vue-i18n Github and I 'have got an error : vue.js:597 [Vue warn]

I am trying to implement vue-i18n Vue-i18n Github and I 'have got an error :

vue.js:597 [Vue warn]: Property or method "$t" is not defined

My vuejs 2 app is working fine until I add the getting starded code, where am I wrong? Thanks in advance

<div id="app">
  <p>{{ $t("message.hello")}}</p>
</div>

<script src=""></script>
<script src=".js"></script>

<script>
  const app = new Vue({
    el: '#app',
    data: {
      products: [
   'Boots',
  ]
   },
  })
</script>
<script>
 // Ready translated locale messages
 const messages = {
  en: {
    message: {
      hello: 'hello world'
    }
  },
  ja: {
    message: {
      hello: 'こんにちは、世界'
    }
  }
  }

 // Create VueI18n instance with options
  const i18n = new VueI18n({
    locale: 'ja', // set locale
    messages, // set locale messages
  })

  // Create a Vue instance with `i18n` option
  new Vue({ i18n }).$mount('#app')
// Now the app has started!
</script>

I am trying to implement vue-i18n Vue-i18n Github and I 'have got an error :

vue.js:597 [Vue warn]: Property or method "$t" is not defined

My vuejs 2 app is working fine until I add the getting starded code, where am I wrong? Thanks in advance

<div id="app">
  <p>{{ $t("message.hello")}}</p>
</div>

<script src="https://unpkg./vue"></script>
<script src="https://unpkg./vue-i18n/dist/vue-i18n.js"></script>

<script>
  const app = new Vue({
    el: '#app',
    data: {
      products: [
   'Boots',
  ]
   },
  })
</script>
<script>
 // Ready translated locale messages
 const messages = {
  en: {
    message: {
      hello: 'hello world'
    }
  },
  ja: {
    message: {
      hello: 'こんにちは、世界'
    }
  }
  }

 // Create VueI18n instance with options
  const i18n = new VueI18n({
    locale: 'ja', // set locale
    messages, // set locale messages
  })

  // Create a Vue instance with `i18n` option
  new Vue({ i18n }).$mount('#app')
// Now the app has started!
</script>
Share Improve this question asked Apr 11, 2018 at 12:53 Emile CanteroEmile Cantero 2,0636 gold badges26 silver badges48 bronze badges 1
  • Why do you have 2 instance of Vue, both mounting on #app? You can remove the first one and move your data to the one with the translations. – thefallen Commented Apr 11, 2018 at 13:08
Add a ment  | 

1 Answer 1

Reset to default 1

You have to specify i18n in any Vue instance you want vue-i18n to work.

The first instance you have has no i18n specified.

Besides, you have two Vue instances, they don't work together, so what you probably need is just one (with i18n specified).

<div id="app">
  <p>{{ $t("message.hello")}}</p>
</div>
<script src="https://unpkg./vue"></script>
<script src="https://unpkg./vue-i18n/dist/vue-i18n.js"></script>
<script>
  // Ready translated locale messages
  const messages = {
    en: {
      message: {
        hello: 'hello world'
      }
    },
    ja: {
      message: {
        hello: 'こんにちは、世界'
      }
    }
  }
  // Create VueI18n instance with options
  const i18n = new VueI18n({
    locale: 'ja', // set locale
    messages, // set locale messages
  })
  // Create a Vue instance with `i18n` option
  const app = new Vue({
    el: '#app',
    i18n, // this is equivalent to `i18n: i18n,` (without quotes, naturally)
    data: {
      products: [
        'Boots',
      ]
    },
  })
  // Now the app has started!
</script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745670502a4639374.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信