I am encountering a recurring issue with my code where it keeps showing a 'not defined

Previously, this code was working fine, but now it has stopped functioning and I can't figure out why. I've tried reorganizing things and changing values, but I keep encountering the same error. If someone could point out where to add a line, that would be really helpful. I'm stumped because I have everything mentioned in methods and get the value during the button click.

const {
  createApp
} = Vue
createApp({
  data() {
    return {
      mins: 0,
      cats: 0,
      xp: 0,

      buttons: [{
        name: "Gym",
        increaseValue: 10

      }, {
        name: "Sleep",
        increaseValue: 2
      }, {
        name: "Eat",
        increaseValue: 3
      }]
    }
  },
  methods: {
    increaseXp(increaseValue) {
      this.xp += increaseValue
    }
  }
}).mount('#app')
<head>
  <script src="https://unpkg.com/vue@3"></script>
  <script src="https://cdn.tailwindcss.com/"></script>
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Name</title>
</head>

<body class="bg-sky-100">
  <div id="app">
    <div class="max-w-3xl mx-auto">
      <h1 class="font-bold text-7xl border-b-4 pb-4 border-black w-fit text-center font-mono">Name of App</h1>
      <h2 class="font-bold text-2xl text-center mt-5">
        LEVEL
      </h2>
      <h3 class="text-center">
        {{xp}}
      </h3>
    </div>
    <div class="grid grid-cols-3 gap-3 px-3 mt-6">
      <button onClick="increaseXp(button.increaseValue)" v-for="button in buttons" class="py-2 border border-black rounded bg-sky-300">
                {{button.name}}
            </button>
    </div>

  </div>
</body>

Answer №1

Switch to using Vue's @click directive instead of the standard HTML attribute onClick.

const { createApp } = Vue
createApp({
    data() {
        return {
            mins: 0,
            cats: 0,
            xp: 0,
            buttons:[{
                name: "Gym", 
                increaseValue: 10
            },{
                name: "Sleep", 
                increaseValue: 2
            }, {
                name: "Eat",
                increaseValue: 3
            }]
        }
    },
    methods: {
        increaseXp(increaseValue){
            this.xp += increaseValue
        }
    }
}).mount('#app')
<head>
    <script src="https://unpkg.com/vue@3"></script>
    <script src="https://cdn.tailwindcss.com/"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Unique Title</title>
</head>
<body class="bg-sky-100">
    <div id="app">
       <div class="max-w-3xl mx-auto">
        <h1 class="font-bold text-7xl border-b-4 pb-4 border-black w-fit text-center font-mono" >Name of App</h1>
        <h2 class="font-bold text-2xl text-center mt-5">
            LEVEL
        </h2>
        <h3 class="text-center">
            {{xp}}
        </h3>
        </div>
        <div class="grid grid-cols-3 gap-3 px-3 mt-6" >
            <button @click="increaseXp(button.increaseValue)" v-for="button in buttons" class="py-2 border border-black rounded bg-sky-300">
                {{button.name}}
            </button>
        </div>
    </div>
</body>

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 on using CSS to hide elements on a webpage with display:none

<div class="span9"> <span class="disabled">&lt;&lt; previous</span><span class="current numbers">1</span> <span class="numbers"><a href="/index/page:2">2</a></span> <span class="num ...

The automatic width feature in Firefox is malfunctioning and not behaving as expected

Here is my question: I have an image wrapped in a div (.indexWrap): <div id = "slider"> <div class="indexWrap"> <img class="indexImage" src=""> </div> ...

Exploring the dynamic animation of jQuery slideDown with the layout manipulation of

My current project involves using an HTML table with multiple rows. I have hidden every second row, which contains details about the preceding row, using CSS. Upon clicking the first row, I am utilizing jQuery's show() function to display the second ...

Adding JavaScript in the code behind page of an ASP.NET application using C#

Currently, my challenge involves inserting a javascript code into the code behind page of an asp.net application using c#. While browsing through various resources, I stumbled upon some solutions provided by this website. Despite implementing them as inst ...

sophisticated mongoose search utilizing $where clause

Dealing with a complex mongoose query has been giving me a headache. As someone who is still new to it, I was hoping for some suggestions on how to solve this issue. The gallery collection I am working with has the following fields: status (with value ...

What is causing the discrepancy in height between my parent DIV and its children?

I've been grappling with this issue for quite some time now and unfortunately, I haven't been able to come up with a solution. Within my design, I have a frame that consists of a top box, a left box, a right box, and a middle box that contains th ...

Only one bootstrap collapse is visible at a time

Currently, I am using Bootstrap's collapse feature that displays content when clicking on a specific button. However, the issue I am facing is that multiple collapses can be open at the same time. I want to ensure that only one collapse is visible whi ...

The image is being loaded onto the canvas and is being resized

Currently, I am facing an issue with loading images into a canvas element as the image seems to be scaled in some way. To provide some context, I have multiple canvases on my webpage and I want to load images into them. Since the dimensions of the canvas ...

Guide to creating a secure login page with the help of cookies

I'm in need of a quick guide on how to implement a login page for a website using cookies. Every user has a unique username and password. Is it necessary to store both the username and password in the cookies, or is just the username sufficient? Is ...

Verifying user credentials using Ajax

I am attempting to implement Ajax for the validation of a username and password stored in a php file on a server. The credentials are pre-defined in the document itself. Within my HTML page, there are fields for entering the username and password. When th ...

Guide to activating a reaction following an occurrence in a React component

I have started developing a React application that loads blog posts along with their associated comments. The challenge I am facing is how to trigger a refresh of the comments component and display the new comment immediately after it has been submitted. ...

Using @at-root seems to be causing an error when combining the "a" selector with

I encountered an issue when attempting to use @at-root and Interpolation in my Sass code. Despite using a standard example for testing, I still receive an error: .button { @at-root a#{&} { color: green; } } Error sass/Site.scss (Line 28 of s ...

Utilize a single function to toggle the visibility of passwords for multiple fields upon clicking

Is there a way to optimize the function used to hide passwords for multiple fields when clicked? Instead of having separate functions for each field, I would like to have one function that is only active on the clicked button. For example, if the toggle ...

Having some trouble getting the text to float alongside an image properly in HTML and CSS

I'm having trouble positioning my text next to an image. I've tried floating it to the right and even using a flexbox, but nothing seems to be working well. Here is the code I have... <div class="tab-content"> <div clas ...

What is the method to verify if a pop-up browser window has completed its loading process?

There is a link on my website that opens a new window. However, sometimes the new window takes so long to load. To prevent users from clicking on the link before the new window finishes loading, I want to disable it until then. I am aware that one way to ...

Is it possible to reuse a variable within a single HTML tag when using Angular 2?

I encountered a strange issue with Angular 2 that may be a bug. I noticed that I couldn't print the same variable in a template twice within the same HTML tag. When I tried to use the following code, it resulted in error messages. <div class=" ...

Populate DataTable with HTML content by fetching it through Ajax requests

My goal is to dynamically load the HTML returned from a controller into a div when a user clicks on a row in a table. Check out my code snippet below: // Add event listener for opening and closing details jQuery('#exRowTable tbody').on ...

Converting JavaScript CanvasRenderingContext2D states array into a serialized format

Looking for a way to serialize a canvas' state in order to save it to a database for later restoration. Want to store the data as an object rather than a bitmap file. I've tried using .save() and .restore() on the context object, but they only m ...

Downloading a folder in Node.js using HTTP

Currently facing an issue with downloading a folder in node js. The problem arises when I make an HTTP request for a whole folder and receive a stream that contains both the folder and files. Existing solutions (like fs.createWriteStream) only seem to work ...

Transform Objects Array from AJAX Response into a Distinct JSON Entity

I am encountering a problem with a sample endpoint that is returning [object Object] for JSON data, and I can't figure out why. Mock API Initially, my code was a bit confusing, but fortunately, I found a clearer solution in another answer. functio ...