Error: Vue Prop is undefined and cannot be utilized within a v-for loop when using TypeScript and decorators

Hey there, I'm currently utilizing Vue along with typescript and facing an issue with props in v-for where it's not rendering anything. Check out the code snippet below for reference

I've experimented with computed props, setting default values to the list, and even creating a new variable instantiated in various lifecycle hooks like constructor or mounted, but the rendering only works when I explicitly define the list like this:

@Prop() list!: string[] = ['foo', 'bar']

The error message I encounter is: "Property or method 'list' is not defined on the instance but referenced during render." The styles for the menu are rendered, but the actual list items are missing:

<template>
  <div class="home">
    <window-menu :list="['foo', 'bar']" />
  </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import WindowMenu from "@/components/WindowMenu.vue";

@Component({
  components: {
    WindowMenu
  }
})
export default class Home extends Vue {}
</script>

<template>
  <div class="window-menu">
    <ul class="window-list">
      <li v-for="(item, index) in list" v-bind:key="index">{{item}}</li>
    </ul>
  </div>
</template>

<script lang="ts">
import { Prop, Vue, Watch } from "vue-property-decorator";

export default class Menu extends Vue {
  @Prop() private list!: string[];
}
</script>

Answer №1

Resolved the issue by adding the @Component decorator which was missing initially, leading to Vue compilation errors for props. See corrected code snippet below:

<script lang="ts>
import { Prop, Vue, Component } from "vue-property-decorator";

@Component
export default class Menu extends Vue {
  @Prop() private list!: string[];
}
</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

Implementing scroll-based animations in VUE.JS with a similar effect to wow.js

Is there a way to incorporate animation into certain blocks while scrolling on my Vuejs website similar to wow.js? If you have any suggestions, I would greatly appreciate it. Thank you! ...

Secure access to an API using a certificate within a Vue.js application running on localhost

My vue.js app is built using vue-cli. The application is hosted at dev.example.com and the REST API can be found at dev.example.com/api/v1/. The backend has added an SSL certificate for security on the development environment. However, when I try to make a ...

Making changes to the DOM within the mounted method will not be reflected in the JEST

Within a Vue component, the mounted method contains the following code: this.el = d3.select(this.$el); this.svg = this.el.select('svg') .attr('width', mainSvgPos.svgWidth) .attr('height', mainSvgPos.svgHeight) ...

Next js is throwing an error because it cannot accept objects as a React child. Instead, it found an error message stating "Response not successful: Received status code 401."

This app showcases Github issues by utilizing the graphql API. After finishing the app, I encountered an error without making any changes. The technologies used for this project include Next.js, Typescript, Material UI, Tailwind CSS, and GraphQL. https: ...

Obtaining distinct form control values for replicated form fields with Angular

Issue with Dynamic Form Duplicates: I am currently working with a reactive form that has two fields - name and value. There is an add button that duplicates the form by copying these fields. The challenge I am facing is with updating the values of the dup ...

Guide on creating multiple instances of vue-multiselect with a simple button click

I am trying to implement a vue-multiselect dropdown with the addition of a new dropdown upon clicking an "add more" button. However, I am currently unsure of the best approach to achieve this. Problem/Question: When adding 2 dropdowns, if the same option ...

TypeScript throws an error if trying to access an Object variable using a String

While the code below is functioning as intended, I am encountering an error in the VS Code Typescript compiler stating that "Type 'String' cannot be used as an index type". Oddly enough, using a string literal instead of a variable like ...

Implementing MouseEvents in Typescript React without having to pass them down to child elements

Is it possible to use Mouse Events on a whole React Element without having to pass it to a child element? I have been passing my handleEvent function to several functional components and now I want to know if it can be done without causing a TypeScript err ...

Tips for including a decimal point in an angular reactive form control when the initial value is 1 or higher

I am trying to input a decimal number with 1 and one zero like 1.0 <input type="number" formControlName="global_velocity_weight" /> this.form = this.fb.group({ global_velocity_weight: new FormControl(1.0, { validators: [Valida ...

Angular 7: The Pitfalls of Incorrect URL Concatenation

After navigating to a different page within my project, an unexpected 404 error with the wrong URL appears in the console. Here's what it looks like: https://localhost:4420/example.com/api/customers However, it should actually look like this: h ...

How do you trim a string and display the final 3 characters?

When dealing with a list of objects, I want to ensure that the chain of tasks does not become too long and break the table or appear aesthetically unpleasing. Therefore, my goal is to trim the tasks and display only the last 3. In the image below, multiple ...

Display notification if the request exceeds the expected duration

Is there a way to display a message when a request is taking too long? For instance, if the request exceeds 10 seconds in duration, I want to show a message asking the user to wait until it finishes. fetchData(url, requestParams) { return this.restServic ...

What is the purpose of adding "/dist" to the library import statement?

Currently, I am in the process of developing a react component library using vite as my primary build tool. After successfully compiling the project and deploying it to the npm registry, I encountered an issue when importing it into my client app. Specifi ...

How to retrieve a random element from an array within a for loop using Angular 2

I'm in the process of developing a soundboard that will play a random sound each time a button is clicked. To achieve this, I have created an array within a for loop to extract the links to mp3 files (filename), and when a user clicks the button, the ...

Guide to dynamically insert rows in HTML using Vue.js based on the selected option

This is the initial row. Depending on the selection made here, different rows will be displayed <div class="row"> <div class="col-md-4"> <div class="form-group label-floating"> <label class="control-label">Case Type< ...

Incorporating components into a pre-existing webpage without utilizing npm

As a beginner in Vue.js and someone who primarily works in a traditional LAMP environment, I have noticed that the vue components I've attempted to use are not working as expected. Can anyone assist me with: Offering guidance on installing vue compo ...

How to Delete Multiple Rows from an Angular 4 Table

I have successfully figured out how to remove a single row from a table using splice method. Now, I am looking to extend this functionality to remove multiple rows at once. html <tr *ngFor="let member of members; let i = index;"> <td> ...

Unexpected behavior: Promise.catch() fails to catch exception in AngularJS unit test

During the process of writing Jasmine unit tests for my Typescript app and running them via Resharper, I encountered an issue with executing an action when the handler throws an exception: describe("Q Service Test", () => { var q: ng.IQService; ...

Tips for correctly retrieving the current state from a pinia store to use as an argument for an asynchronous function

I am currently facing an issue with a simple counter store in pinia. The counter itself updates and displays correctly on the page, however, when trying to use the count in a specific function, it does not reflect the updated state of the counter store. Ho ...

Guide on utilizing tslint in conjunction with npx

I currently have tslint and typescript set up locally on my project. In order to run tslint against the files, I am using the following command: npx tslint -c tsconfig.json 'src/**/*.ts?(x)' However, when I run this command, it seems to have no ...