What could be causing the VueJS function not to be triggered by the dropdown selection?

I created a simple component called LocaleSwitcher.vue using the Element UI library:

<template>
  <el-dropdown trigger="click">
    <div>
      <i class="el-icon-map-location"></i>
    </div>
    <el-dropdown-menu>
      <el-dropdown-item @click="switchLocale('ru')">
        Русский
      </el-dropdown-item>
      <el-dropdown-item @click="switchLocale('en')">
        English
      </el-dropdown-item>
    </el-dropdown-menu>
  </el-dropdown>
</template>

<script>
export default {
  name: "LocaleSwitcher",
  methods: {
    switchLocale: function(locale) {
      console.log(locale);
      if (this.$i18n.locale !== locale) {
        this.$i18n.locale = locale;
      }
    }
  }
};
</script>

In my view, I include it like this:

<template>
  <div class="login-container">
    <el-form>
      <div class="title-container">
        <h3 class="title">{{ $t("loginPage.formTitle") }}</h3>
        <LocaleSwitcher class="set-language"></LocaleSwitcher>
      </div>
  </div>
</template>

<script>
import LocaleSwitcher from "@/components/LocaleSwitcher";

export default {
  name: "Login",
  components: {
    LocaleSwitcher
  }
};
</script>

The component is visible on the page with no errors, but when selecting a language, nothing happens - the selection is not even logged to the console.

Answer №1

Are you utilizing Element? Check out the documentation for "Command event".

The el-dropdown component includes a @command event that fires when a dropdown item is selected. The value of the command attribute on the el-dropdown-item is passed as the first argument.

<template>
  <el-dropdown trigger="click" @command="switchLocale">
    <div>
      <i class="el-icon-map-location"></i>
    </div>
    <el-dropdown-menu>
      <el-dropdown-item command="ru">
        Русский
      </el-dropdown-item>
      <el-dropdown-item command="en">
        English
      </el-dropdown-item>
    </el-dropdown-menu>
  </el-dropdown>
</template>

<script>
export default {
  name: "LocaleSwitcher",
  methods: {
    switchLocale: function(locale) {
      console.log(locale);
      if (this.$i18n.locale !== locale) {
        this.$i18n.locale = locale;
      }
    }
  }
};
</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

exploring numerous boxes in an engaging and educational tutorial

Explaining this in the title was a bit tricky, but what I want to create is an interactive tutorial. In this tutorial, the user will click on an area or product they want to clean or use for cleaning. After clicking, another box with relevant information w ...

Tips on appending a parameter to an image URL using JavaScript

On my website, I am able to insert images with specified width and height using this code: <img src="image.php?w=100&h=100"> I would like the image size to change based on the device screen width. For screens smaller than 600px, I want to displa ...

"Identifying Mouse Inactivity in React: A Guide to Detecting When the Mouse

I need to dynamically control the visibility of a button element based on mouse movement. I am able to show the button when the mouse is moving using onMouseMove, but I'm stuck on how to hide it when the mouse stops moving. React doesn't have an ...

Tips for invoking an asynchronous function within an if condition?

When trying to maintain variables in the background.js of a Chrome extension, I encountered difficulties that require me to reinitialize some global variables. Here is the code snippet (view fiddle) I am using to demonstrate the issue: var temp = null; ...

How can the data from a factory be utilized within the same controller but in a separate file in an AngularJS application?

When trying to route from home.html to profile.html, there seems to be an issue with the execution of the profile.js script. I have data in script.js that is written in a factory. When the profile button is clicked, the page routes to profile.html, but the ...

Transmit analytical data to an external domain without expecting a reply

Initial Conditions I have ownership of mysite.com I lack ownership of othersite.com, but I am able to embed javascript code on that site Query What is the method to transmit analytic data from othersite.com to mysite.com ? ...

Develop interactive, reusable custom modals using React.js

I am currently working on creating a reusable modal: Here is my component setup: class OverleyModal extends Component { constructor(props) { super(props); } openModal = () => { document.getElementById("myOverlay").style.display = "blo ...

Tips for generating a <div> element with multiple children using a loop

section, I have configured a div element with a class named div class = "postWindow". When it comes to the HTML code, here is an example: <div class = "postWindow"> <div class = "userName">Initial Name</div> <div class = "p ...

Unit test: Passing a deeply nested object as a prop to a React component

My functional component looks like this: const ImageScreen = (props: any) => { const images: object[] = []; props.navigation.state.params.images.forEach((image: any) => //some code ); return ( <View style={CommonStyles.normalPage} ...

Tips on presenting our data using JSON structure

Hey there! I'm new to Next JS and I'm looking to showcase the data from my index.js file in JSON format in my dynamicid.js file. Can anyone guide me on how to achieve this? index.js In my index.js file, I currently display my output but now I ...

The 'xxxxx' term cannot be found within an undefined value when using the 'in' operator in Vue.js

Does anyone have experience creating a modal form for adding a record? I am able to display default values from data, but whenever I try to modify a field, I encounter the following error: *vue.min.js:6 TypeError: Cannot use 'in' operator to se ...

Processing two Array Objects can be achieved without resorting to the EVAL function

I have two objects that I need to process. obj1 contains an array of objects with formulas. obj2 holds the values needed for the calculations. I am looking for a way to process and calculate both objects in order to obtain a result where the keys present ...

Transforming the code from numerous div elements to utilizing Ajax for efficient execution

One of the key functionalities is that the content within the <div> changes based on the option selected in the <select> element. I am looking to incorporate ajax instead of multiple <div> elements. However, my proficiency with ajax is l ...

Can I create interactive stacked shapes with CSS and/or JavaScript?

Trying to explain this may be a challenge, so please bear with me. I need to create an "upvote" feature for a website. The number of upvotes is adjustable in the system settings. The upvote controls should resemble green chevrons pointing upwards. For exa ...

Experiment with catching an exception on variable `v`

In my code, I am using a v-if statement to display an error message in HTML. <div id="error" v-if="showError">Error User or Password</div> data() { return { showError: false, };} When I change the value of showError ...

Cypress - A Guide to Efficiently Waiting for the Outcome of a Javascript Function Import

I am interested in creating a Javascript library to act as a wrapper for 3rd party APIs. I have decided to write the API wrapper as a standalone file rather than using Cypress Custom functions, so that I can share the library with teams who are not using C ...

Creating a connection to an external website through a JavaScript function within an Angular application

I am currently working on an Angular application. Within the index.html file, there is a header that contains links to external websites. <a href="#" onclick="getExternalUrl('about.html');">Click here </a> <scr ...

Modifying array elements in JavaScript without explicit instructions

I am relatively new to Javascript and seem to be encountering a rather puzzling issue that I'm unable to resolve. Currently, I have two classes: Country and PPM_Data. The Country class contains parameters such as name, area, ppm, etc., along with a f ...

Array of JSON data passed in the request body

Recently, I have been attempting to pass JSON data to my req.body. The data structure is as follows: answers = ["A","B"]; //An array to be included in the JSON Object var Student_Answers = { //JSON object definition Answers: answers, matricNumber: ...

Creating a sticky footer using Vue.js for both mobile and desktop display views

Looking for a solution to create a sticky footer that works on both desktop and mobile views without overlapping the main content? The current code either overlaps or doesn't stick to the bottom. Seeking a responsive approach. App.vue: <template& ...