Unable to invoke a mixin function within a JavaScript function

I've been experimenting with a small project utilizing nuxt.js, and currently, I've developed a plugin using mixins as shown below. However, I'm puzzled as to why function b is not working inside the render function:

import Vue from 'vue'

Vue.mixin({
  methods: {
    a () {
      const render = function () {
        this.b()
      }
      render()
    },

    b () {
      alert('testme')
    }
  }
})

Answer №1

By utilizing the function keyword to define the render function, the this within it points to the calling context (a), which does not have a property named b.

To resolve this issue, it is recommended to switch to an arrow function:

const render = () => this.b();
.

const methods = {

  a() {
    const render = () => {
      this.b()
    }
    render()
  },

  b() {
    alert('testme')
  }

}

methods.a()

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

What could be causing my onscroll function to cut off before reaching the end of the element?

Struggling with a function I created to slide a div horizontally into view in the center of a vertically scrolling page. It seems to work well under normal conditions, but when the page is scrolled quickly, it sometimes fails to complete the horizontal mov ...

Implementing dynamic data binding in JavaScript templates

I've been experimenting with jQuery and templates, and I managed to create a basic template binding system: <script type="text/template" id="Template"> <div>{0}</div> </script> Furthermore... var buffer = ''; v ...

What is the reason behind Chrome Dev Tools not automatically adding the parentheses when a method is selected?

In the console of Dev Tools, if you have an object named x with three methods/functions - a(), b(), and c(i, j, k), why doesn't it automatically insert the parentheses, along with the correct spaces for the parameters (similar to eclipse for Java) whe ...

Adding a prefix to the imported CSS file

My React app, created with create-react-app, is designed to be integrated as a "widget" within other websites rather than functioning as a standalone application. To achieve this, I provide website owners with minified JS and CSS files that they can inser ...

Retrieve the data of the current component from the slot template

This specific vue component showcases a CardGroup template with a headerRight slot. The content of the slot includes a message displaying the total number of items, accessed through the this.total data property. <template> <CardGroup> ...

Express is unable to locate the specified property

Here is my controller code snippet: exports.showit = function(req, res){ res.render('showpost', { title: req.post.title, post: req.post }) } In my post model, I have included title and name objects: title: {type : String, default : &apos ...

Finding the identifier for resources through excluding external influences

I am currently facing an issue with the full calendar plugin. In my set up, I have 3 resources along with some external events. The problem arises when I try to drop an external event onto the calendar - I want to retrieve the resource id from which the ev ...

Flow object with Angular using ng-flow: Existing flow object

Just a quick question that I can't seem to find an answer for on my own or through searching online. Currently, I have a button set up for clicking and uploading files. However, I also want to incorporate drag and drop functionality using the same fra ...

Replicate and modify the settings on a fresh radio inspection

In my approach, I am avoiding direct HTML editing and instead utilizing JavaScript/jQuery to accomplish the desired outcome. Initially, one input (specifically 'Express Shipping') is pre-selected by default. The goal is to clone/copy the HTML co ...

Issue with JQuery on Mobile Devices: Troubles with Dropdown Menu and Loading Function

Having some trouble with my JQuery code on mobile devices. The drop down menu isn't showing up on an iPhone, the load function isn't working to display the additional PHP page on a Samsung Edge 7, and the drop down doesn't seem to be functio ...

HapiJS commences an extended duration background process

Is there a way to achieve the functionality of a PHP exec function in HapiJS? I have a scenario where the user submits a processing job that requires running in the background for a significant amount of time. An essential requirement is to provide the us ...

Is it possible to provide unrestricted support for an infinite number of parameters in the typing of the extend function from Lodash

I am utilizing the "extend" function from lodash to combine the objects in the arguments as follows: import { extend } from 'lodash'; const foo1 = { item: 1 }; const foo2 = { item: 1 }; const foo3 = { item: 1 }; const foo4 = { item: 1 }; const f ...

Creating Bound HTML inside an AngularJS Grid Cell Template

I recently started using angular js and came across a helpful question on Conditional cell template in ui-grid angularjs. It worked perfectly for me, but now I'm curious about how to display HTML elements like a button tag instead of plain text. Whene ...

Having trouble with your jQuery AJAX function not receiving the text returned from your PHP file?

After extensive searching, I have come across several individuals facing the same issue as me, but unfortunately, none of them seem to have found a solution. The problem at hand is that PHP is not providing any data to the AJAX function. When I try to dis ...

Ways to activate a dialog box by clicking on a card without triggering the dialog when clicking on its child elements

I'm currently using the most recent version of Vuetify and I'm looking to trigger a dialog when I click on a card. However, the issue is that this particular card contains multiple child elements (such as a form) and my intention is for the dialo ...

Aligning text vertically to the top with material UI and the TextField component

Seeking guidance on adjusting vertical alignment of text in <TextField /> const styles = theme => ({ height: { height: '20rem', }, }); class Foo extends React.component { ... <TextField InputProps={{ classes: ...

Is there a way to determine if the validityMessage is currently being displayed on a DOM element using reportValidity()?

When reportValidity() is called on an invalid field, the validation message is displayed in a manner that varies depending on the browser. However, if reportValidity() is called again, the validation message disappears on Safari but not on Firefox. Contin ...

What is preventing me from utilizing automatic reloading in Next.js to view changes without the need to restart the server?

Being a beginner in Next.js and the world of web development, I may have some basic questions. Following the installation guide, I managed to successfully create my first Next.js app. However, I'm facing an issue with auto-reloading when I make change ...

Retrieve information from Google Sheets to use in SVG map

While working on a local HTML page, I encountered an issue with my script. I am using the svgMap library to create a map of movies I have seen, pulling data from a Google Sheets document using the opensheet library. The JSON output looks like this: [{"Coun ...

The Lerna command for adding packages fails with an error message stating that the packages are not related

Currently, I am utilizing lerna to manage a multirepo containing interrelated packages. This issue only arose after installing a new operating system. Whenever I attempt to utilize lerna add to add a dependency from one package to another, an error occurs ...