Getting the height of a div in a Nuxt.js application results in an error alert being displayed

Trying to retrieve the height of a div within a grid in a Nuxt application.

<template>
  <div class="grid grid-cols-3">
    <client-only>
      <Item
        v-for="(
        item, itemIndex
        ) in ItemsArray"
        :key="`item-${itemIndex}`"
        v-bind="item"
        :ref="`item${itemIndex}`"
      />
    </client-only>
  </div>
</template>

<script>
export default {
  mounted() {
    this.itemHeight = this.$refs.item1.clientHeight
  },
  data() {
    return {
      itemHeight: 0,
    }
  }
}
</script>

An error message shows up saying

Cannot read properties of undefined (reading 'clientHeight')
. Need help on resolving this issue...

Answer №1

The reason behind this is that when you incorporate ref within a v-for loop in Vue, it results in an array of refs instead of individual ref elements.

Although I don't have access to my computer right now, based on my memory, you will need to implement something similar to the following:

mounted() {
  this.itemHeight = this.$refs.item1[0].clientHeight
},

If this solution doesn't work for you, try using console.log(this.$refs.item1) for debugging purposes to observe how the structure is organized. This way, you can determine the solution on your own.

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

Receiving a 401 error while attempting to make an axios get request with authentication headers

I have been utilizing axios in my React project to fetch data from MongoDB. However, I am facing a challenge with the axios get requests returning a 401 error when I include my auth middleware as a parameter. This middleware mandates that the user must pos ...

The art of Vue JS displaying content

Learning how to make API requests using axios has greatly improved my ability to retrieve data in a client-side container and display it in my component template. It's been quite effective so far. Having worked with twig before, I can't help ...

The data provided was deemed incorrect. Modifying the function to include file uploads in Laravel 8 and Vue 2

I've been attempting to create an update function that includes file upload capability. Here is what I have experimented with so far: <b-form @submit.prevent="update" enctype="multipart/form-data"> . . . . <b-form-f ...

Submitting forms with CakePHP and AJAX

I'm struggling with implementing ajax and jQuery functions. I need to update my database table based on checkbox values. Below is the code snippet from a ctp file: $(".chk").click(function(e){ e.preventDefault(); var id = $(this ...

What is the best way to create a sidebar that remains open without triggering a new sidebar every time it is clicked

I am looking to add a sidebar that opens a new view without navigating to a different page. The sidebar remains consistent and I want to avoid duplicating it on all pages. Check out this photo for an example. My goal is to provide additional content or fe ...

Exploring Vue 3: Crafting a custom plugin using the composition API and enhancing it with Typescript type augmentation

Encountering an issue with displaying plugins properly within <script> and <template> tags on WebStorm. Firstly, let's take a look at my files and configuration: tsconfig.config.json { "extends": "@vue/tsconfig/tsconfig. ...

Navigating a local and server environment with relative paths in a web server's multiple sites

My ASP.NET website is published to a web server with multiple sites, such as www.example.com/SiteA or www.example.com/SiteB. Before publishing, I test the site locally at localhost:12345. When referencing an image path like /Images/exampleImage.gif, it wo ...

I am trying to showcase a collection of images on my homepage, but unfortunately, it is not functioning as expected

Does anyone know how to display images using Angular? I am currently utilizing the pic controller in an AngularJS file. Any assistance would be greatly appreciated. HTML CODE <!DOCTYPE html> <html> <head> <meta charset="utf-8"& ...

How can I combine all the areas in an ExtJS Area Chart with varying opacity levels instead of stacking them on top of each other?

I'm currently working on an area chart in ExtJS 4.1 and so far, everything is being displayed correctly. My issue is that one area is overlapping another in the chart, which I would like to avoid. Is there a way to overlay all the areas instead? Perh ...

When the toggle enabled div is clicked, the drag event is triggered

My draggable div has a "splitter" for expanding and collapsing, but it should only do so on double click or drag. However, when I single click the splitter while the div is collapsed, it repositions to around 10px width. I've attempted using 'st ...

Exploring the contrast between Vuex store WATCH and SUBSCRIBE

Can you explain the main distinction between watch and subscribe, and when it is most appropriate to use one over the other? According to information on the Vuex official documentation, both methods appear to be essentially identical in functionality and p ...

Trigger next animation after the completion of current animation using Jquery animate callback

Is there a simpler way to achieve this task? var index = 0; while (index < 5) { $(this).find(".ui-stars-star-on-large:eq(" + index + ")").animate({ width: w + 'px' }, 200, "swing"); index++; } ...

Challenges encountered when passing objects with Angular 2 promises

I am encountering a problem when using a promise to retrieve a Degree object in Angular 2. The initial return statement (not commented out) in degree.service functions correctly when paired with the uncommented implementation of getDegree() in build.compon ...

Positioning tooltip arrows in Highcharts

I'm attempting to modify the Highcharts tooltip for a stacked column chart in order to have the arrow on the tooltip point to the center of the bar. I understand that I can utilize the positioner callback to adjust the tooltip's position, but it ...

Storing user input in MongoDB after encoding

I am currently exploring the most effective methods for storing and presenting user input in MongoDB. In traditional SQL databases, it is necessary to encode all user input as a precaution against injection attacks. However, in the context of MongoDB, ther ...

Ways to extract variables from a component and utilize them in App.js

Despite my efforts to search online, I could not find the answer or understand what I needed to. My issue: I need the "inputVal" variable from the "InputField.js" component to be accessible in the "App.js" component (specifically in the fetch request where ...

Using Typescript to iterate through an array of objects and modifying their keys using the forEach method

I have an object called 'task' in my code: const task = ref<Task>({ name: '', description: '', type: undefined, level: 'tactic', participants: undefined, stages: undefined, }); export interface Tas ...

Encountering an issue such as "Exceeding the maximum number of re-renders. React restricts the amount of renders to avoid

Currently, I am attempting to update the state within the request data method for a string variable as shown below: const ViewChangeRequest = () => { const [requestStageValue, setRequestStage] = useState(''); const { data: request ...

Divide an array into smaller arrays

If I create a function named sliceArrayIntoGroups that takes an array and a size as parameters, how can I divide the input array into smaller arrays of the specified size? function sliceArrayIntoGroups(arr, size) { var slicedArray = arr.slice(0, size); ...

Curious about retrieving a value in a Bootstrap confirm modal?

Although it may seem like a silly question, I am struggling to figure out how to wait for the confirmation button to return a value in Javascript. Since Javascript is single-threaded, I need to implement a callback function that will wait until a button is ...