I need to retrieve my array from the return() function within the setup() function

My issue involves accessing an array named Title in the data() method, where values are dynamically added. I am trying to access this Title array within the onDrop() method inside the setup() function. However, I keep receiving an error stating that it is undefined. Can anyone help me resolve this problem?

import Home from "./components/Home.vue";
import Section from "./components/Sections.vue";

export default {
  name: "App",
  components: {
    Home,
    Section,
  },
  data() {
    return {
      Title: [],
    };
  },
  methods: {
    sectionTitle(Title) {
      const newSectionTitle = {
        Title: Title,
      };
      this.Title.push(newSectionTitle);
    },
    onremoveSection(Index) {
      this.Title.splice(Index, 1);
    },
  },
  setup() {
    const startDrag = (event, index) => {
      console.log(index);
      event.dataTransfer.dropEffect = "move";
      event.dataTransfer.effectAllowed = "move";
      event.dataTransfer.setData("TitleIndex", index);
    };
    const onDrop = (event, indx) => {
      console.log(indx);
      const index = event.dataTransfer.getData("TitleIndex");
      console.log(index);

      let indexToMoveFrom = index;
      let indexToMoveTo = indx;
      const shifter = (Title, indexToMoveFrom, indexToMoveTo) => {
        let buff = Title[indexToMoveFrom];
        for (let i = indexToMoveFrom; i < indexToMoveTo; i++) {
          Title[i] = Title[i + 1];
        }
        Title[indexToMoveTo] = buff;
        console.log(Title);
      };

      shifter(Title, indexToMoveFrom, indexToMoveTo);
    };
    return {
      startDrag,
      onDrop,
    };
  },
};

Answer №1

If you want to organize your data and methods, consider moving them to the setup function:

import { ref } from 'vue'
export default {
  name: "App",
  components: {
    Home,
    Section,
  },
  setup() {
    const title = ref([])
    const sectionTitle = (Title) => {
      const newSectionTitle = {
        Title: Title,
      };
      title.value.push(newSectionTitle);
    }
    const onremoveSection = (Index) => {
      title.value.splice(Index, 1);
    }
    const startDrag = (event, index) => {
      console.log(index);
      event.dataTransfer.dropEffect = "move";
      event.dataTransfer.effectAllowed = "move";
      event.dataTransfer.setData("TitleIndex", index);
    };
    const onDrop = (event, indx) => {
      console.log(indx);
      const index = event.dataTransfer.getData("TitleIndex");
      console.log(index);

      let indexToMoveFrom = index;
      let indexToMoveTo = indx;
      const shifter = (Title, indexToMoveFrom, indexToMoveTo) => {
        let buff = Title[indexToMoveFrom];
        for (let i = indexToMoveFrom; i < indexToMoveTo; i++) {
          Title[i] = Title[i + 1];
        }
        Title[indexToMoveTo] = buff;
        console.log(Title);
      };

      shifter(Title, indexToMoveFrom, indexToMoveTo);
    };
    return {
      title,
      startDrag,
      onDrop,
      sectionTitle,
      onremoveSection
    };
  },
};

</script>

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 process of flattening an array in Zustand: a comprehensive guide

In my array of brands, each brand object includes a brand name and products. Initially, I successfully added a new brand object to the brands list. However, when attempting to add another brand while keeping the previous brand data intact, it does not fu ...

The beforeEach hook in Mocha.js does not support the bail(false) functionality

Whenever I attempt to initiate my mocha test with the instruction bail(false), I am looking to ensure that the tests do not halt even if an error is encountered in a beforeEach hook. Despite setting this configuration, it seems like it's not working ...

Data is successfully being stored in an array in AngularJS, however, it is not appearing in the user interface

Having an issue with displaying updated data on my UI. I am successfully pushing data into the database and an array using Angular 2-way binding. The data is being pushed as confirmed by the console, but it's not showing up on the user interface. Con ...

What is the best way to implement a sub-menu using jQuery?

After successfully implementing a hover effect on my menu item using CSS, I am now struggling to make the sub-menu appear below the menu item upon hovering. Despite my efforts to search for jQuery solutions online, I have not been successful. Are there a ...

Is Local Storage compatible with phonegap?

I am looking to integrate local storage into my phonegap application in order to store an array of data. Here is a snippet of my code: <div data-role="collapsibleset"> <div data-role="collapsible"> <h3>LOG ...

How should one go about creating an npm package out of a vuejs component and testing it locally?

Initially, I created a vuejs project as a test container using vue-cli. Next, I developed an npm package named "vue-npm-example" from a Vuejs component in my local environment and then imported it into the aforementioned testing project. Within the packag ...

Unable to load page redirection

I am experiencing an issue with this page not redirecting to the appropriate mobile or desktop page when accessed. Below is the code snippet in question: <html> <head> <title>Loading...</title> </head> < ...

What could possibly be causing my app to exhaust CPU resources on Mozilla Firefox?

I have created a unique game application for Facebook. Currently, the app is not optimized with AJAX technology, resulting in multiple server requests causing high CPU usage (specifically in Firefox) which slows down the overall performance of the app. Alt ...

Is there a way to deactivate a clickable div immediately after it has been clicked on?

I have been searching for a solution to this particular question on different platforms, including Stack Overflow. However, I am looking for an answer using pure JavaScript only, so please avoid jQuery solutions. In order to provide context, I have includ ...

Retrieving text content from multiple classes with a single click event

There are numerous elements each having class names p1, p2, p3,...p14. Consequently, when attempting to extract text from the clicked class, text from all classes is retrieved! For instance, if the expected text is 80, it ends up being 808080080808080808 ...

Using the OR Operator with a different function in React

Struggling with setting the day flexibility using disableDate(1,2,3,4,0) but it's not functioning as expected. Can you assist me in fixing this issue? Here is the function snippet: const disableDate = (date) => { const day = date.day(); retur ...

Unable to substitute a value using the useState hook in React

Whenever I click a key, I attempt to update the value of a variable but it appears not to be working as intended. ↓ The current implementation is not effective and needs improvement import React, { useState, useEffect } from 'react'; const Li ...

Creating a custom event handler for form input changes using React hooks

A unique React hook was created specifically for managing form elements. This hook provides access to the current state of form fields and a factory for generating change handlers. While it works seamlessly with text inputs, there is a need to modify the c ...

Tips for troubleshooting an Express application launched by nodemon through a Gulpfile in WebStorm 10

I have a unique Express application that is powered by a Gulpfile configuration. gulpfile.js 'use strict'; var gulp = require('gulp'); var sass = require('gulp-sass'); var prefix = require('gulp-autoprefixer'); va ...

Filtering a collection using another collection in Backbone: tips and tricks

I am managing two distinct collections: Collection X includes element1, element2, element3, element4. Collection Y consists of element2, element3. For illustration purposes: var element1 = new models.ExModel({id: "1", name: "element1"}); var elemen ...

Combining two objects in node-red: A step-by-step guide

msg.payload : Object object HAMK307: object a_temperature: 23.1 a_humidity: 46 a_brightness: 3703.05 a_lights: "on" a_presence: "empty" r_temperature: 35.59 r_humidity: 30.46 r_pressure: 994.43 a_time: object ID: "HAMK-307" m ...

Troubleshooting Issue with Mongoose Virtual Field Population

I am currently facing an issue with my database due to using an outdated backend wrapper (Parse Server). The problem arises when dealing with two collections, Users and Stores, where each user is associated with just one address. const user = { id: &q ...

Trouble with AJAX communicating with PHP and not rendering fresh data

I have created a row of images that are clickable, and once clicked, a variable is sent via AJAX to a PHP file for a database query. The PHP file receives the variable and executes the correct query. However, instead of updating the HTML on my page, it sim ...

Traditional Javascript is unable to add elements to the DOM

I am currently facing an issue with my website that is built in Expression Engine. The back-end of the website contains a code snippet responsible for handling a JavaScript request and generating a page based on that request. One of the challenges I am en ...

Conceal a section if the array is not defined

Hey there, I've got this piece of code for checking the status of a Twitch streamer. $(document).ready(function () { // some initializations here var login = ''; var twitchStatusLinks = $('.twitch-status'); var twitchStatus ...