Is there a way to transform vanilla JavaScript code into Vue.js code?

// Vanilla JS Code Conversion
const app = new Vue({
  el: '#app',
  methods: {
    // Logged out Events
    loginHelp: function() {
      this.$refs.forgotten.style.display = 'flex';
      this.$refs.login.style.display = 'none';
      this.$refs.signup.style.display = 'none';
    },
    forgottenBack: function() {
      this.$refs.forgotten.style.display = 'none';
      this.$refs.login.style.display = 'flex';
      this.$refs.signup.style.display = 'none';
    },
    signupBtn: function() {
      this.$refs.forgotten.style.display = 'none';
      this.$refs.login.style.display = 'none';
      this.$refs.signup.style.display = 'flex';
    },
    signupBack: function() {
      this.$refs.forgotten.style.display = 'none';
      this.$refs.login.style.display = 'flex';
      this.$refs.signup.style.display = 'none';
    }
  }
});

This is my attempt to convert vanilla JavaScript code to Vue.js. I created a new Vue instance and defined methods for handling the logged out events. However, I encountered some errors while trying to implement this conversion. Can you help me debug and successfully convert this code?

<script>
// @ is an alias to /src
import HelloWorld from "@/components/HelloWorld.vue";

export default {
  name: "Home",
  methods: {
    // Method declarations here
    
  },
  components: {
    HelloWorld
  }
};
</script>

Answer №1

When working with Vue files, it's important to add click events in the template section rather than in the script (js) section. This is commonly done like so:

 <button @click="yourFunction">Click me</button>

In your methods, you would then have:

 methods: {
   yourFunction() {
     this.addClass = true;
   },
  } 

There are various ways to add styles, one of which includes binding a class:

 <button :class="{'hidden': addClass}"  

Make sure to have a variable like "addClass" in your data section:

  data() {
    return {
     addClass: false,
    };
  }  

For more information, consult the official documentation at: https://v2.vuejs.org/v2/guide/events.html and https://v2.vuejs.org/v2/guide/class-and-style.html

Answer №2

To make certain actions happen, you must associate classes accordingly: Vue binding classes and styles The classes you link should be specified in data() and then attach events to HTML elements to toggle these classes or modify them as needed

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 the reason for receiving a Firebase INVALID_DYNAMIC_LINK_DOMAIN error message?

I'm having trouble implementing email verification in my React website. Whenever I use the sendSignInLinkToEmail function, I encounter the following error: XHRPOSThttps://identitytoolkit.googleapis.com/v1/accounts:sendOobCode?key=xxxxxxxxxxxxxxxxxxx [ ...

Executing a JavaScript function within a Vue component script

I'm working on a simple component file for submitting a form, along with a JavaScript function to handle an action: <template> <div> <div class="modal-header"> <button type="button" class="close" data-dismi ...

Ways to steer clear of using eval for converting strings to decimal values

I currently have the fraction "1/7" and I am looking to convert it into a decimal. While I know I can achieve this using eval("1/7"), I prefer not to use eval as it is considered harmful. Are there any alternative methods to accomplish this? ...

Implement jQuery to dynamically assign an "Active" class to tab elements based on the current page being loaded

INQUIRIES I have include: How do I apply a class to an element using jQuery, or any other method, for the active tab? Ensure that the dropdown tab appearing is the one containing the active tab, not always the Company one. In essence, I want the ac ...

Guide to transferring a JavaScript/Ajax variable to a PHP webpage with the help of AJAX

function initiateIperfSpeedRequest(speedVar, actualIp) { var xmlhttp = getAjaxObject(); xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { handleIperfResponse(xmlhttp.respo ...

Utilizing AJAX to retrieve data from a PHP function

Seeking to display the output of an AJAX call; This is my PHP code: if(isset($_POST['date']) ) { echo "<input type='radio' id='date'/>"; } And here's my AJAX code: $.ajax( { type: "POST", url: "sched ...

Vue Tab doesn't properly initialize Vue Chart.js

In my component, I have two vue-tabs with two instances of vue-chart-js each. While they initialize without errors, attempting to extract an image from a chart in the inactive tab using document.querySelector('#mySecondChart').toDataURL() results ...

Executing functions during the page loading process

I'm currently in the process of integrating a new payment method into a checkout page that does not have built-in support for it. The button on the page that redirects to an external payment portal requires specific parameters to be passed. I've ...

requiring a page reload for an AJAX-loaded webpage

Encountering an issue with a third-party integration on a website specifically designed for iPads, where multiple pages are loaded using AJAX. Upon visiting the page for the first time, the expected functionality is missing and only appears after refreshi ...

Having issues with Vue.js, facing difficulty in displaying array fetched from axios.get request

My attempt to showcase information from GitHub issues involves using axios to make a GET request to the REST API. I am trying to retrieve data, for instance, from this GitHub API link. Here is my .vue file content: <template> <h1>List< ...

Class for making elements draggable using jQuery UI

Is it possible to use jQueryui's draggable/droppable combo and add a class to the dragged item when dropped into a specific container, rather than adding a class to the container itself? I've tried adding a class to the container, but that is not ...

How can I retrieve the height of a hidden image in Internet Explorer?

I am encountering an issue with images that have been set to display:none. I am using javascript (document.getElementById('elem').height) to retrieve the height/width of these images. While this method works in most browsers, IE is reporting th ...

Issue with modal rendering in Bootstrap4 when the body is zoomed in

I am encountering an issue with a Bootstrap html page where the body is zoomed in at 90%. The Bootstrap modal is displaying extra spaces on the right and bottom. To showcase this problem, I have created a simple html page with the modal and body set to 90% ...

The jstree does not seem to be generating the tree structure as expected based on

I am utilizing the jstree plugin to construct a hierarchical tree view of locations, rooms, and assets for a company within a PHP application that I am developing. The main intention behind this tree is to enable users to select an asset while going throu ...

When using Next.js and Express.js together, CORS error may occur, causing API queries to only function properly during build

I am currently working on a project that involves using Next.js for the front-end and Express.js for the back-end. Front-end Setup The 'pages' directory contains an 'index.js' file where I have implemented the following code snippet: ...

Start up Angular with a Fire $http.get when the page loads

I am facing an issue where my $http.get() request is firing after the home page has already loaded. How can I ensure that the request fires before the page loads so I can utilize the returned data on the page? Here is a snippet of the routing code: var l ...

What are the different kinds of properties that can be used in Vue.js

When working with Javascript, I can create arrays that hold different types of data elements like: var ex = ['name', 12, true]; console.log(ex); In Vue JS, when defining props for a component within a single file template, I have the option to ...

"Step-by-Step Guide: Implementing a Modal Popup Form in Angular with NgBootstrap and FormsModule

I am seeking assistance with my Angular project. I am attempting to implement a Modal Popup Form using NgBootstrap and FormsModule, but encountering issues with the NgbModule not being imported. Here are some details of my setup: node 16.15.1 cli 15.1.5 co ...

Angular Tip: Display data in a dropdown using a select element

When I have two select elements and want to display values if they exist: page.ts import { Component } from '@angular/core'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: ['./app ...

Is the jQuery AJAX call using POST method being retrieved as $_GET?

Below is a snippet of code that I've successfully implemented: <script type="text/javascript"> $(document).ready(function() { // Initializing the table $('#table_1').tableDnD({ onDrop: function(table, row) { $.tab ...