The function was not invoked as expected and instead returned an error stating "this.method is not

I need help with an issue in my index.vue file. The problem lies in the fancy box under the after close where I am trying to call the messageAlert method, but it keeps returning this.messageAlert is not a function

Can someone please assist me in resolving this?

<template>
  <div>
    <v-btn @click="openCal">Open Now</v-btn>
   </div>
</template>

<script>
  export default {
    methods: {
      messageAlert() {
        alert("Method is called")
      },
      
      openCal() {
            let src = 'https://google.com/'

            $.fancybox.open({
              type : 'iframe',
              scrolling : 'yes',
              src  : src,
              opts : {
                afterShow : function( instance, current ) {
                  $("body").attr("style", "overflow: hidden; position: fixed;")
                    $(".fancybox-content").attr("style", "height: 90% !important; overflow: scroll;")
                   $(".fancybox-content > iframe").attr("style", "height: 101% !important; overflow: scroll;")
                    $(".fancybox-content > iframe").attr("scrolling", "yes")
                  
                },
                afterClose : function( instance, current ) {
                  $("body").attr("style", "")
                  this.messageAlert();
                  
                }
              }
            });
      }
    }
  };
</script>

Answer №1

The reason for this behavior is the scope of the function; afterClose : function has its own scope (this), which means it does not have access to the messageAlert function. One solution is to make it an anonymous function:

afterClose : ( instance, current ) => {
  $("body").attr("style", "")
  this.messageAlert();
}

Alternatively, you can pass an instance:

<v-btn @click="openCal(messageAlert)">Open Now</v-btn>
...
openCal(callback) {
  ...

  afterClose : function( instance, current ) {
    $("body").attr("style", "")
    callback();
  }

Answer №2

Instead of relying solely on Tina's solution, consider storing the component's context in a variable.

launchCalendar() {
        let reference = this

        $.fancybox.open({
          ...
          opts : {
            afterClose : function( instance, current ) {
              $("body").attr("style", "")
              reference.displayMessage();
            }
          }
        });
}

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

Using ThreeJS to calculate the rotation needed for one Vector3 to align with another Vector3

Recently delving into the world of ThreeJS, I've been exploring its potential for fun and creativity. My current project involves using ThreeJS in conjunction with aframe. The task at hand requires computing with ThreeJS to pass position and rotation ...

Progress bar displaying during Ajax request

I'm currently working on an Ajax request that uploads images to the Imgur API and I want to implement a progress bar to show users the upload progress. I found some guidance in this thread, but it seems to display only 1 and stop working. This is the ...

Automatically populate form fields with data from the clicked row in the HTML table when using JSP

Attempting to populate form fields using jQuery or JavaScript with row elements selected by clicking on the row. Tried a solution from Stack Overflow that didn't work as expected. I'm new to this, so please bear with me. (http://jsbin.com/rotuni/ ...

Node.js has no trouble loading HTML files, however, it seems to encounter issues when trying to

Having a bit of trouble with my JavaScript skills as I try to load my index.html file (seems like it should be the 5th easiest thing to do in JavaScript). Let me get straight to the point; when I manually open my index.html, it loads perfectly WITH the CS ...

Strange behavior noticed in the app.get() method of Node.js Express

Seeking clarification regarding the behavior of app.get() in Express. It appears that the function is not triggered when the path includes .html at the end. In the code snippet provided, the console logs "test" if attempting to access /random/example, bu ...

What's the issue with my ExpressJS req.query not functioning as expected?

My NodeJS repl setup includes an input, button, and h1 element. The goal is to update the HTML inside the h1 element with the value of the input upon button click. Here's a glimpse of my code: index.js: const Database = require("@replit/database ...

What is the method for retrieving a module from a store using its name represented as a string?

Just starting out with Vuejs and encountering a slight hiccup. I want to utilize a store, however, all I have is the name stored as a string. From what I understand, I can access the method (all()) of the store by using: storeName.all My issue lies in ha ...

Vue-Router does not currently have built-in support for the dynamic import() function

Encountering an issue with the code below indicating "unexpected token import": const Router = new VueRouter({ routes: [ { path: '', component: () => import('pages/landing/landing') } ] }) A solution that is currently ...

Experimenting with an Angular Controller that invokes a service and receives a promise as a

I am currently in the process of testing an angular controller that relies on a service with a method that returns a promise. I have created a jasmine spy object to simulate the service and its promise-returning method. However, my mock promise is not retu ...

Utilize Vuex to modify information in the database

I've been facing some challenges while trying to incorporate an EDIT_DETAILS feature in vuex. Although I can achieve this without using vuex, I'd rather utilize vuex as a means to enhance my skills in it. Here are the code snippets that I am cur ...

Vue Page fails to scroll down upon loading

I am facing a challenge with getting the page to automatically scroll down to the latest message upon loading. The function works perfectly when a new message is sent, as it scrolls down to the latest message instantly after sending. I've experimented ...

Unable to access a hyperlink, the URL simply disregards any parameters

When I click an a tag in React, it doesn't take me to the specified href. Instead, it removes all parameters in the URL after the "?". For example, if I'm on http://localhost:6006/iframe.html?selectedKind=Survey&selectedStory=...etc, clicking ...

"AngularJS Bi-Directional Data Binding Issue: Unexpected 'Undefined

Recently, I've been tackling a project that involves using a directive. Initially, everything was smooth sailing as I had my directive set up to bind to ngModel. However, I hit a roadblock when I needed to bind multiple values. Upon making the necessa ...

Move each four-character IT value to a new line

const maxNumLength = 4; event = jQuery.Event("keypress") event.which = 13 $('#input').on('input focus keydown keyup', function() { const inputValue = $(this).val(); const linesArray = inputValue.split(/(&bsol ...

Dynamically load modules within an AngularJS application

Is there a way to dynamically load module scripts? I have 2 JS files: module1.js (function() { var mod = angular.module('module1', []); .... })(); This is the second one: module2.js (function() { var mod = angular.module('m ...

Verify changes in the Cross Domain RSS feed by utilizing Ajax technology

I have a website where I am trying to automatically reload an RSS news feed from every 60 seconds if it has been updated. I attempted to use Ajax for this purpose, but I seem to be facing some issues: <script type="text/javascript" src="../js/jquery.a ...

What sets index.js apart from page.js in next.js?

https://nextjs.org/docs/pages/building-your-application/routing/pages-and-layouts While reading through the next.js documentation, I came across an interesting point. The documentation mentions that index.js serves as the root of the directory. This mean ...

Change the display of the lightbox when clicked. Utilize Angular and JQuery for this functionality

Here is the code for a lightbox: <div id="light-box"> <div id="first"> ..... </div> //initially visible <div id="second"> ..... </div> //hidden - but displayed when button is clicked. </div> I want to add two button ...

Sending JSON data to the server using jqGrid: A step-by-step guide

[CHANGE] (I couldn't bear to wait 3 hours for an answer): It seems that the issue is not with the jqGrid component, many thanks to TheCodeDestroyer for identifying this. I ran this code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "h ...

Adjusting column widths in Material-Table: A guide to resizing columns

I am currently using the Material Table library, recommended by Google Material UI as a top data table library. I am facing some issues related to configuring the width of columns in this library. The column's `width` property seems to be functioning ...