Troubleshooting why the second statement is not being triggered by the Vuejs

Incorporating a lambda expression into the methods section of a Vuejs component has been a recent challenge for me.

Here's an example: I initiate alertyou() and upon receiving the alert, I click okay. Subsequently, in the Vue developer tools, I notice that this.activated transitions to true.

export default {
  name: 'HelloWorld',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App',
      activated: false
    }
  },
  methods: {
      alertme: () => { alert('Clicked'); this.activated = false; },
      alertyou() {
        alert('Alert You');
        this.activated = true
      }
  }
}

Yet, upon clicking the button that triggers the alertme lambda, I notice that after acknowledging the alert message, this.activated remains true!

What perplexes me is whether this limitation pertains to lambdas. Are we only allowed to execute one statement per lambda? Or could this matter involve scope dynamics post the firing of an alert?

Answer №1

What's happening here? Is this a constraint with lambda functions? Can only one statement be executed per lambda? Or is this related to the scope after an alert is triggered?

Actually, it's neither.

An arrow function maintains the binding of `this` to the context in which it was created. In this scenario, `this` is not the vue instance, but most likely `window`.

A function declared within an object using the function keyword (or ES6 object shorthand) typically has `this` bound to the object where the function is declared.

This explains why you can access `this.activated` in `alertyou`, but not in `alertme`.

To access `alertme`, you should use the same syntax as in `alertyou` to declare the method.

Answer №2

Modify the function from

alertme: () => { alert('Clicked'); this.activated = false; }
to
alertme() { alert('Clicked'); this.activated = false; }
. For a demonstration, check out the live example here

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

The data retrieved from the PHP script is not accessible within the main Vue instance

I am currently working on a Vue.js component for a modal window. Once the user fills out all the necessary data in the fields, I need to achieve the following: Send the data to the server. Apply a timeout to show the user that the data is being sent (by ...

When trying to apply styles using ng-style attribute with jQuery, Angular does not seem to

Check out this plunker showcasing the issue : http://plnkr.co/edit/1ceWH9o2WNVnUUoWE6Gm Take a look at the code : var app = angular.module('myApp', []); app.controller('myCtrl', function($scope) { console.log('yeah'); ...

The error message displayed on the vue-router.esm.js?8c4f:2181 file shows a TypeError, stating that it is unable to read the property '

When attempting to log in using Gmail authentication for my Vue Firebase application, I encounter an issue after clicking on "sign in with Google." Even though I get directed to the dashboard page, I am unable to view its content. In the console, the follo ...

Is there a way to automatically redirect my page after clicking the submit button?

I'm having trouble with the code below. I want it to redirect to NHGSignin.php if 'new horizon gurukul' is entered. When I click the Next button, it's supposed to take me there but it's not working as expected. Can anyone help me f ...

Retrieve the number of rows in the table component

I'm in need of getting the row count from a table component that I have built using vuejs. My goal is to display the row count outside of the table component structure. The issue with the code snippet below is that it doesn't show the correct row ...

Incorporating traditional Javascript classes for modeling in React development

Can traditional JavaScript classes be utilized in models within the MVC framework while using React, as opposed to relying on Redux or contexts & reducers which may impact reusability? If this approach is feasible, how can we efficiently 'subscribe&ap ...

Issues arise with routing when specific route parameters are implemented

After setting a route parameter in my browser URL, I encountered errors with the routing of the public folder (which contains my CSS, JS, etc.). The app's structure is as follows: app | |-- public | └-- css | └-- profile.css | |-- ...

A dedicated folder for hosting the static assets generated by Nuxt.js

I have a quick question I'm looking to create a dedicated directory for the static files generated by Nuxt Js Currently, Nuxt Js compiles all files into a single directory called 'dist' As I am utilizing Django Server as my backend, I nee ...

Substitute regular expressions with several occurrences by their respective capture groups

I am attempting to use JavaScript to extract only the link text from a string and remove the href tags. The expected behavior is as shown below: <a href='www.google.com'>google</a>, <a href='www.bing.com'>bing</a> ...

"Enhance your webpage with a captivating opaque background image using Bootstrap

I'm new to exploring Bootstrap and I am currently experimenting with options for displaying content with a semi-transparent background image. Currently, I am using a "well" but I am open to other suggestions. I have managed to place the image inside t ...

Creating a dynamic user list display feature in AngularJS similar to WhatsApp's user textbox feature

I am looking to display a list of users when a user presses the @ button in a text box, similar to how WhatsApp shows group members in AngularJS. Here is my html code: <input class="nostyle search-filter" ng-model="searchUsers" type="text" placeholde ...

How to Use Google Calendar API to Retrieve Available Time Slots for a Given Day

Is there a way to extract the list of available time slots from my Google Calendar? Currently, I am only able to retrieve the list of scheduled events. I am utilizing the Google Calendar npm package. google_calendar.events.list(calObj.name,{ timeMin ...

Retrieve JSON information from a document through JavaScript

Is it possible to fetch JSON data using JavaScript without relying on jQuery? I am only interested in retrieving data using pure JavaScript. Here is an example of my JSON file: {"JsonProjectIDResult":[{"_capacity":15,"_description":"Meeting Room","_dev_d ...

Connect ng-include URL to certain paths

I am working with multiple routes in my application: routes.js $routeProvider.when( '/dashboard/one', { templateUrl: 'partials/dashboard.html', controller: 'DashboardCtrl' }); $routeProvider.when( '/da ...

Issue with NextJs function not receiving the specified argument variable

Currently, I am focused on developing a Shopify website and fine-tuning the functionality of the shopping cart. My onClick event triggers a function that initiates the process of adding items to the cart. The first step involves checking if there is an exi ...

Activate validation when the scope of a custom validator is modified

Consider a scenario where we have a custom validator with an input as the attribute's value. app.directive('inputRequired', function() { return { require: 'ngModel', scope: { inputRequired: '=& ...

What is the reason behind the controller being unable to locate an Angular model when it contains dots in its name?

I am completely new to Angular and struggling to comprehend this code snippet. app.controller('FileConfigController-detail', function($scope, $http, $stateParams, FileConfigDetailsService) { $scope.detail.inptITResourceID = "test me" ...

Looking to restrict the data retrieved from an API while utilizing ReactJS

I am currently fetching transaction data from an API. One of the fields in the response is buyer, which may sometimes be null. As a result, I am excluding any entries with a null buyer. This leads to varying numbers of results being displayed. My goal is t ...

What is the correct way to configure Google Analytics on my Vue web application?

I am currently working on a Vue component called calculator.vue and I have included the necessary code for the plugin in my main.js file as shown below: import App from './App.vue' import vuetify from './plugins/vuetify' import "./ ...

Using Node.js Express to showcase a JSON file utilizing Class Methods

Recently diving into node.js and express, I attempted to display a JSON file containing an array of 3 objects using a class method Below is the Class structure: const fs = require('fs') class GrupoArchivo { constructor(filePath) { t ...