What is the process of including data in Vue using refs?

My goal is to click a button and have the page scroll up or down. However, I need references for each span I add in order to include an index. The issue arises when trying to incorporate this index into the method. How can I add data to these references? Any assistance would be greatly appreciated. Thank you, friends.

<template>
    <div id="app">
        <div class="emoji_picker">
            <div class="picker_container">
                <div class="general__smiles"
                     v-for="(emoji,index) in generalSmiles"
                     :key="emoji.id"
                >
                    <button 
                        @click="goToSmiles(index)"
                    >
                        {{ emoji.smile }}
                    </button>
                </div>
                <div class="category"
                     v-for="(category,index) in categories"
                     :key="`category_${category}`"
                >
                    <span
                        v-if="category_emojis(category).length"
                        :ref="'category'+index"  //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                    >
                        {{ category }}
                    </span>
                </div>
            </div>
        </div>
    </div>
</template>


export default {
    data() {
        return {
            generalSmiles: [
                {smile: "๐Ÿ˜€", id: "smile__๐Ÿ˜€"},
                {smile: "๐Ÿต", id: "smile__๐Ÿต"},
                {smile: "๐Ÿ“•", id: "smile__๐Ÿ“•"},
                {smile: "๐Ÿ ", id: "smile__๐Ÿ "},
                {smile: "โœ…", id: "smile__โœ…"}]
        }
    },
    methods: {
        goToSmiles(index) {
            // Issue lies here! How do I resolve this?
            const el = this.$refs.('category'+index)
            if (el) {
                el.scrollIntoView({behavior: 'smooth'})
            }
        }
    }
}

Answer โ„–1

Here is a useful tip:

goToSmiles(index) {
    const el = this.$refs['category' + index][0]
    if (el) {
        el.scrollIntoView({ behavior: 'smooth' })
    }
}
  1. Made a modification to the code by removing the extra dot . after $refs and accessing the property with square brackets using the name 'category' plus the index.
  2. Also included [0] after the ref since Vue refs sharing the same base name are turned into an array.

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

How to set the element in the render method in Backbone?

Currently, I am in the process of developing a web page utilizing BackboneJS. The structure of the HTML page consists of two divs acting as columns where each item is supposed to be displayed in either the first or second column. However, I am facing an is ...

How to update a vuex state from a separate component?

In my current project, I have a modal component that relies on a state management store. This store keeps track of whether the modal is currently active or not. I would like to be able to trigger the opening of this modal from other components or even jus ...

Using Flickity API in Vue 3 with Typescript Integration

I have encountered an issue with implementing Flickity in my Vue 3 application. Everything works perfectly fine when using a static HTML carousel with fixed cells. However, I am facing difficulties when attempting to dynamically add cells during runtime us ...

Tips for adjusting the placement of an object within a table

Below is an example of a basic table: table, th, td { border: 1px black solid; border-collapse: collapse; } <table> <tr> <th>number</th> <th>name</th> <th>id</th> </tr> <tr&g ...

Issue with React not displaying JSX when onClick Button is triggered

I've recently started learning React and I'm facing a problem that I can't seem to figure out. I have a basic button, and when it's clicked, I want to add another text or HTML element. While the console log statement is working fine, th ...

Using Jquery and CSS to add a class with a 1-second delay when the mouse enters

I have successfully implemented the addClass function in my code, but I'm encountering issues when attempting to use delay or setTimeout functions. It's important to note that I'm avoiding the use of webkit attributes in CSS. If anyone has ...

Unable to process image upload due to setState not being recognized as a function in ReactJs

Attempting to implement an upload function using the cropper component. However, encountering an error when trying to set the state: Uncaught TypeError: this.setState is not a function at FileReader.reader.onload Component import React, { Component, P ...

Access and retrieve dynamically generated table row values with the use of AngularJS

Hi, I'm new to angularjs and I have a table where I need to dynamically add rows. I've got everything working with a bit of JQuery but I'm having trouble getting the value of dynamically created table rows. Here's my code, can someone p ...

Unveiling the essence of Lodash's differenceBy functionality

I am facing a challenge with two arrays of objects. I need to identify the differences between the newData and oldData arrays based on their identifiers. Specifically, I want to display objects from oldData whose identifiers are not present in newData. Her ...

Understanding TypeScript typing when passing arguments to the Object.defineProperty function

After reviewing all the suggested answers, including: in Typescript, can Object.prototype function return Sub type instance? I still couldn't find a solution, so I'm reaching out with a new question. My goal is to replicate Infix notation in J ...

Ways to transfer a function as attributes from one functional element to another?

I've encountered an issue when passing a function as a prop from one functional component (Dashboard) to another (Event). Every time I click the button on the child component that holds the passed function prop in the onClick value, it results in an e ...

Simulating a traditional table scroll function on a window using virtualization

I am currently attempting to incorporate a virtualized table in react using reactโ€“virtualized, but I am facing issues with the rendering of the table. I am seeking to understand the root cause for this problem as well as find solutions for other overlapp ...

Animating content to slide into view in the same direction using CSS transitions

My goal is to smoothly slide one of two 'pages', represented as <divs>, into view with a gentle transition that allows the user to see one page sliding out while the other slides in. Eventually, this transition will be triggered from the ba ...

Initiate Vue Modal Displaying a Message

I need to implement a modal window similar to the functionality of a normal alert() Currently, I am utilizing bootstrap-vue BModal How can I create a Modal class in my code and trigger it? Alternatively, should I include the modal in the root app.vue fi ...

Encounter an issue while attempting to generate a multidimensional array in JQuery

I am trying to utilize jQuery to create a multi-dimensional array. Below is the code snippet I have written for reference: GiftData = []; GiftData['boxProduct'] = []; GiftData['boxName'] = jQuery('#giftbox-data .box-data').te ...

Ways to integrate PHP MySQL with NodeJS and SocketIO

Currently, I am working on developing a chat application. I have successfully implemented features like creating accounts, logging in, selecting, viewing, and more using PHP MySQL. Now, I am venturing into the Instant Messaging aspect by utilizing NodeJS a ...

Is it possible to pass the chart type as a property when using vue-chart.js?

Is it possible to pass the chart type as a property with vue-chart.js? I would love to see some example source code. Can you please provide guidance on this? ...

The Problem of Unspecified Return Type in Vue 3 Functions Using Typescript

Here is the code snippet I am working with: <template> <div> <ul v-if="list.length !== 0"> {{ list }} </ul> </div> </template> < ...

Make sure to load the HTML content before requesting any input from the user through the prompt function

Could you please help me with a question? I'm looking to load HTML content before prompting the user for input using JavaScript's prompt() function. However, currently the HTML is only loaded after exiting the prompt. Below is the code that I hav ...

Persistent change issue with JQuery CSS function

Looking for some help with a button-bar div that I want to display only after the user clicks on a "settings" button. Currently, I have set the button-bar div to have a display:none property in my css file. However, when the settings button is clicked, t ...