What could be the reason for the list being undefined even though I explicitly defined it within the <script setup> section of my Nuxt 3 Project?

I am currently working on a Nuxt 3 Project and have created a component that generates a variable amount of elements. When calling the element, it is passed an array as a parameter. In the script setup, I define this array as 'list' and intend to use it within the mounted function to map specific data points onto my data.

Below is an example of the component in use where the array is passed to the element. Each of the variables (x.data.attr..) is a String.

<Menu
  :items="[
    {
      bildURL: x.data.attributes.BildKasten1.data.attributes.url,
      Text: x.data.attributes.TextKasten1,
      KartenTitel: x.data.attributes.KartenTitel1,
    },
    {
      bildURL: x.data.attributes.BildKasten2.data.attributes.url,
      Text: x.data.attributes.TextKasten2,
      KartenTitel: x.data.attributes.KartenTitel2,
    },
  ]"
/>

My Setup:

<script lang="ts" setup>
import { faThList } from '@fortawesome/free-solid-svg-icons';

const props = defineProps({
  items: {
    Array,
  },
});

const list = ref(
  props.items.map((item) => {
    return { ...item, open: false };
  }),
);
</script>

Within the normal script tag (I removed the methods since they are not relevant here):

<script lang="ts">
export default {
  data() {
    return {
      obj: {},
    };
  },
  mounted() {
    console.log(list);
    for (i in list) {
      this.obj[i.KartenTitel] = idx > 0 ? false : true;
    }
  },
};
</script>

Unfortunately, the code is not working as expected and the console displays the error message:

Uncaught (in promise) ReferenceError: list is not defined

The most frustrating part is that it was working yesterday when I turned it off, but today it's not. There haven't been any changes made in between. Why isn't it working now? Shouldn't 'list' be defined since it is declared within the setup?

Answer №1

As outlined in the Vue 3 documentation, it is advised to incorporate the composition API within the options API solely with the setup() function, particularly if you are transitioning from an existing options API codebase (instead of two <script> blocks):

<script>
  export default {
    setup() {
      // composition API
    },

    // continue with options API here
  }
</script>

Alternatively, you should stick to using just one of the APIs.

In your scenario, you could consider implementing onMounted(() => {}) within the <script setup> block instead.

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

Access user connections through Discord.js bot

Hi there, I'm currently working on creating a bot that can retrieve a user's connected battle.net account and display their game rank. I am utilizing the discord.js library and have been attempting to access the UserProfile through the bot. Unfor ...

Having trouble with ES6 in Canvas - why won't my code display correctly?

I'm currently working on developing a painting app using ES6. However, I'm facing issues with the positioning and line drawing on the canvas. The lines are not being drawn in the correct position; for example, the top-left corner is formed when ...

Parent menu fails to trigger the opening of child menu upon clicking

I'm struggling to modify a Wordpress theme to enable the child menus to expand when clicking on the parent menus. Is there a way to edit this code to make that functionality work? // Implement expandable menus var expand_link = $('<a class="m ...

What is the best way to set the theme for Material-UI in a React application?

I find myself puzzled when it comes to styling a front-end based on Material-UI. Can someone clarify where the theme originates from in the following example code and what impact the theme has? import React from "react"; import Container from "@material- ...

React and Material-UI issue: Unable to remove component as reference from (...)

My React components are built using Material-UI: Everything is running smoothly MainView.js import React, { Component } from 'react'; import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider'; import { List, ListItem } from ...

Tips on utilizing CSS modules in React without changing class names

After starting to use css modules in my react project, I quickly realized the struggle of changing classnames to fit the requirements of css modules. For example, if we have a component using regular css: import React from 'react' import ". ...

The magic of Angular's data binding post-ajax retrieval

My situation is as follows: /items/item/123/edit and I have a controller that combines both the view and edit functionalities: ... if ($routeParams.id) { $scope.itemId = $routeParams.id; $scope.editMode = true; Item.getB ...

The file raphael.2.1.0.min.js contains a UTF-8 byte sequence that is not valid

Currently, I am attempting to implement the justgage plugin on my Rails 4 application. However, an error has arisen: raphael.2.1.0.min.js contains an invalid UTF-8 byte sequence The only changes I made to my application.js were: //= require justgage.1 ...

Display a JSON object on a web browser

I am trying to display a JSON object on a web browser using HTML. The object is already in a text file and has been properly formatted for readability. My goal is to maintain the same formatting when displaying it on the browser. ...

What is the best way to retrieve the offsetHeight of a Component Element using Vue.js?

I am currently working on a Vue.js component and successfully inserting it into the DOM. My goal is to find out its rendered height once it's in the DOM, specifically its offsetHeight. However, I seem to be missing something obvious as I can't fi ...

The spinning loading wheel on Firefox persists even after submitting the iFrame

I have encountered a strange issue with Firefox that I can't seem to figure out. In my system, AJAX is used to send data to a PHP API. However, when file uploads are involved, Firefox does not use XMLHttpRequest() and instead reverts to submitting the ...

jQuery animation not executing as expected due to transition issues

I'm looking to create a cool effect where the previous quote disappears and a new one appears when I click on a button, all with a smooth transition effect. In my attempt below using jQuery .animate and opacity property, I'm struggling to make i ...

What steps should I take to export a function from a React functional component in order to create a reusable library?

Currently, I am in the midst of developing a React component library and one of my components contains a function that I want to export. The purpose of the addParticle function is to enable users of the library to dynamically insert particles into a cont ...

What is the process for setting default parameters using a recompose, lifecycle HOC?

I've created a custom recompose, lifecycle HOC: import { lifecycle } from 'recompose'; export function myHoc(title) { return lifecycle({ componentDidMount() { console.log(title) } }); } export default my ...

How do I specify a unique directory for pages in Next.js that is not within the src or root folders?

I'm currently facing an issue when trying to set a custom directory in Next JS. Although the default setup dictates that the pages directory should be located at the root or within the src directory, this arrangement doesn't fit my requirements ...

What could be the reason for Vue's ref not functioning properly with Set data type, but working perfectly fine with integer

There seems to be an issue with the watch function when using Set, but it works fine with int. Switching from ref() to reactive() resolves the problem. Is this behavior expected? <script setup> import { ref,watch,reactive } from 'vue' cons ...

Encountering a reload error while refreshing the Angular page

Whenever I click on a deck from my list, the corresponding deck-detail component is supposed to load and display the details of the selected deck. The URL should also change to something like "deck/id/deckName". However, if I try to reload the page or copy ...

"Freezing issue with Angular big table causing web page to lock up during

Looking for a way to improve the performance of an HTML table search function that is currently running very slowly and freezing the web page upon loading and searching. The table needs to be within the HTML file itself, not pulling data from a server. Any ...

Unable to retrieve any values using the for each loop in AngularJS

Having an issue with my Angular.js foreach loop. When I use console.log('course data',response.data); the output looks like this: course data [Object, Object, Object] 0: Objectcourse_name: "Master of computer 1: Objectcourse_name: "Bachelor of ...

Shorten the content while preserving the HTML using JavaScript

When printing a string containing HTML links, I need to truncate the visible text while preserving the link structure. Simply using a substring method would disrupt the HTML tags in the string. I aim to display only 100 characters but also remove any inc ...