Vue.js is notifying that the property or method "$t" has not been defined

I am attempting to integrate vue-i18n from the Vue-i18n Github repository and encountering an error:

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

My Vuejs 2 application was functioning properly until I added the initialization code. Can anyone point out where I may have made a mistake? Thank you in advance.

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

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

<script>
  const app = new Vue({
    el: '#app',
    data: {
      products: [
        'Boots',
      ]
    }
  })
</script>
<script>
 // 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
  })

  // Initialize Vue instance with `i18n` option
  new Vue({ i18n }).$mount('#app')
// Application is now running!
</script>

Answer №1

In order for vue-i18n to function properly, you must include the i18n parameter in each Vue instance.

Your initial Vue instance does not have the i18n parameter specified.

Additionally, if you have two separate Vue instances that are not working together, it is recommended to consolidate them into one instance with the i18n parameter included.

<div id="app">
  <p>{{ $t("message.hello")}}</p>
</div>
<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for retrieving scattered objects from array fields based on the identifiers stored in the JavaScript array

Within my database, there exists a collection called SetVoca, with the following structure: { "_id" : ObjectId("5e63518c3e5d0d08980e2203"), "lessonId" : "5e63518c3e5d0d08980e2201", "vocaList" : [ { "_id" : ObjectId("5e6351 ...

Adjust 2 scroll bars simultaneously within Vuetify

I am currently working on a Vuetify project where I have two DIV elements in a splitpane, each wrapped in their own scroll panes. I am trying to find a way to make both scroll panes scroll at the same time or somehow link/bind one scroll bar to another. h ...

Remove search results in real-time

I'm currently working on implementing a search feature for a web application. While I have made some progress, I am facing an issue with removing items when the user backspaces so that the displayed items match the current search query or if the searc ...

The specified variable will not be visible in the window object

I recently created a JavaScript code snippet that looks like this: let var1 = 1; window.var2 = 2; After running the code in the Chrome console, I entered window to inspect the global window object. Surprisingly, only the second variable appeared and the ...

Performing a search in Select2 only after all remote data has been loaded

The select2 feature is functioning almost as desired. It successfully loads all remote data and formats it correctly when another field is changed. However, I am looking to reintroduce the search function for the retrieved data. Specifically, once the data ...

The nested promise.all function is executing synchronously instead of asynchronously as anticipated

Within the nested Promise.all section of my NodeJs script, I believe there is an error. The script is designed to process multiple .csv files within a directory, creating a database record for each row. If a row fails processing, it is added to a list of f ...

Is there a way to retrieve all documents based on the start and end of a specific day?

One common issue I am facing involves submitting a date to my nodejs server in a specific format

 2018-11-02T00:36:00+05:30 // The actual time should be 12:36AM However, when examining the document in my database (using studio 3T), the format appear ...

Leveraging Bootstrap grid system within AngularJS elements

I am currently working on wrapping grid element divs into Angular components in order to streamline the input process and establish a standard: <bootstrap-row> <bootstrap-input-text col=6 ng-model="$ctrl.model" label="hey!"& ...

instead of downloading the file, click on the provided download link to open it directly

Currently, I am utilizing Python Selenium to web scrape images in Chrome. To complete the download of these images, I generate download links using the subsequent code snippet: script_js = 'var imageURL = document.getElementsByTagName("{select_ta ...

Converting a Java Object array to a JavaScript Object Array when passing it as a

Is there a way to send a Java array of objects to a JavaScript function? Here is an example object: class SitioMapa{ String title; double lat; double lng; String description; public SitioMapa(String title,double lat,double lng,String ...

What is the root of the error message "Name is not defined" in this specific Angular 11 application?

Currently, I am developing a project using Angular 11. Within the UserService service, the following code is implemented: import { Injectable, OnDestroy } from '@angular/core'; import { UserModel } from '../path/to/UserModel'; export ...

Populate a text field with a selected value from a dropdown menu

How can I populate a text field with a value from the database when an option is selected from a combo box? Note: both the values in the combo box and the one I want to show in the text field are fetched from the same table in the database. ...

The .css() method does not apply any styles to the elements in the document

Having trouble with a jQuery function: .css() This is the HTML code in question: <div class="..." id="about"> // OTHER HTML CODE </div> And here's the corresponding jQuery code: (function($, window, document) { $(function() { ...

Setting default values or placeholders for dependent select lists is a common and useful feature that can improve the

Having four dependent select lists, I aim to assign a default value/placeholder like select ... to all the select lists. Nevertheless, the issue arises when attempting it using <option value=""> Select ... </option>, as changing the first selec ...

The function is invoking itself, with no recursive calls present within it

I'm working on a function to retrieve shop data from a MongoDB database. exports.find_shops = function(selector, fields, limit, skip, cb){ if(typeof fields == 'function'){ limit = 0 cb = fields fields = {} ...

Changing time format from ISO time to short time in JavaScript

I am working on an ajax call that retrieves a time in the format 16:06:59, and I need to convert it to 4:06 PM. var mydate = obj[0].time; The variable mydate contains 16:06:59, but when I try to use it with var date = new Date(), it returns today's ...

iPhone App Freezes when trying to download a file through Phonegap

Encountering an issue while downloading a file with phonegap: if the internet connection is lost, the application hangs and crashes. The error message received is: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ...

Executing Numerous Post Requests concurrently from a single origin

I am currently working on a project that requires me to utilize a rest API backend server (NodeJs/Typescript) by making multiple simultaneous post requests from my frontend. Although this may seem unusual, it is a specific requirement for the project. Upo ...

Tips for determining if a key is present in local storage:

I need to set a key value, but only if it doesn't already exist. In my component1.ts file, I am assigning the key and value in the constructor. However, I want to include a condition that this action should only be taken if the key is not already pre ...

The Challenge of Dynamic Angular Components

Upon triggering the blur event of the contenteditable div, I observe changes and dynamically generate new strings with additional spans to update the same div accordingly. However, a challenge arises when all text is deleted from the contenteditable div, r ...