The problem with Vue 3 loop iteration

I am facing an issue while attempting to loop through the data from the App.vue component by passing props to a child component. When I try to display the contents, I encounter an error in the v-for loop. Upon reviewing the code and error message, it seems like there is a problem with how I accessed the 'job' property. The error message states: "Property 'job' does not exist on type '...'. Did you mean 'jobs'?" As a beginner, I'm unsure of how to resolve this issue. Any suggestions?

<template>
        <div> 
            <ul>
                <li v-for="job in jobs" :key="job.id"></li>
                <h2>{{job.title}} in {{job.location}}</h2> //Error
            </ul>
            <div class="salary">
                <p> {{job.salary}} rupees </p> //ERROR
            </div>
        </div> 
    </template>
    
    <script lang="ts">
    import { defineComponent, PropType } from 'vue'
    import Job from '@/components/Job'
    
    export default defineComponent({
        props: {
            jobs: {
                required: true,
                type: Array as PropType<Job[]>
            }
        }
    
    })
    </script>
    
    <style scoped>
    
    </style>

Answer №1

To ensure that the data inside the loop is displayed in multiple locations, you must enclose the data that needs to be within the loop inside the v-for directive. Here is an example:

<template>
        <div v-for="job in jobs" :key="job.id"> 
            <ul>
                <li></li>
                <h2>{{job.title}} in {{job.location}}</h2> //Error
            </ul>
            <div class="salary">
                <p> {{job.salary}} rupees </p> //ERROR
            </div>
        </div> 
    </template>
    
    <script lang="ts">
    import { defineComponent, PropType } from 'vue'
    import Job from '@/components/Job'
    
    export default defineComponent({
        props: {
            jobs: {
                required: true,
                type: Array as PropType<Job[]>
            }
        }
    
    })
    </script>
    
    <style scoped>
    
    </style>

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 missing properties in the TS Type are as follows:

Currently working with Angular 7.x.x and TypeScript version 3.2.4. I have defined two TypeScript interfaces where one extends the other: Main interface: export interface Result { var1: string; var2: number; var3: boolean; } The second ...

Comparison of valueChanges between ReactiveForms in the dom and component级主动形

Is there a method to determine if the change in valueChanges for a FormControl was initiated by the dom or the component itself? I have a scenario where I need to execute stuff() when the user modifies the value, but I want to avoid executing it if the v ...

Is there a more efficient method to specify to typescript the type of "data"?

Recently, I've adopted the action/reducer pattern for React based on Kent Dodds' approach and now I'm exploring ways to enhance type safety within it. export type Action = { type: "DO_SOMETHING", data: { num: Number } } | ...

Issue: Unable to locate a differ that supports the object '[object Object]' of type 'object'. NgFor can only bind to Iterables like Arrays

I have successfully pulled data from the jsonplaceholder fake API and now I am attempting to bind it using Angular 2 {{}} syntax. However, I encountered an error that states: "Error: Cannot find a differ supporting object '[object Object]' of typ ...

The attribute 'X' is not present in the 'context | null' type. Error code: ts(2339)

I'm totally puzzled by this issue. Even though I clearly defined the function type in TasksContextType, why is TypeScript throwing an error in my code... Error: Property 'addTask' does not exist on type 'TaskContextType | null'. t ...

I am looking for guidance on how to effectively utilize a JSON object that is stored in the constructor of my component, particularly when triggering

Below is the object I have in my constructor. I am passing a value from a previous component to the one below. I receive the item json object, but I need to use it when I click. constructor(public navCtrl: NavController, public navParams: NavParams) { ...

There is a delay in updating ng-if/ng-hide in real time on the HTML page

Assistance needed for implementing a slight adjustment in AngularJS with TypeScript. The requirement is to change the text of a button for 3 seconds upon clicking, then revert back to its original text. Two HTML elements are created for this purpose, each ...

Exploring the functionality of PivotTable's PivotUI feature in Vue 3

I've been working on implementing pivotUI from pivottable.js in Vue 3, but I encountered an issue since the only available wrapper is for Vue 2. When trying to use it, I keep getting an error message saying "Uncaught ReferenceError: jQuery is not defi ...

Typescript: The type 'Observable<{}>' cannot be assigned to the type 'Observable'

I'm encountering an issue with the Observable type, any thoughts on how to resolve it? import { PostModel } from '../model/postModel'; import { Subject } from 'rxjs/Subject'; import { Observable } from 'rxjs/Observable&ap ...

Indexing types unexpectedly behaving and generating errors that were not anticipated

I find the code below quite strange... export class Collection { private data: {[k: string]: any} = {}; constructor () { // This part works fine this.data["hello"] = "hello"; // Unexpectedly works this.data[2] = 2; ...

Preventing Button Clicks in Angular 2 When Form Data is Modified

I am looking to determine if at least one field in my form has been altered. When this condition is met, the disableButton variable will be set to true, and false if there are no changes in the form. Here is the snippet of code I am currently using: // Th ...

Using Systemjs with Angular 2 results in 50 server calls for loading resources

While following the Angular2 quickstart tutorial on Angular.io, I noticed that it was making 50 separate requests, which left me wondering why. https://i.sstatic.net/bqMk8.png Is there a way to consolidate all these requests into one? My goal is to have ...

When implementing Typescript, utilizing the useContext() function may result in the following error: "Cannot assign type 'never[]' as an argument to the parameter type

When attempting to utilize the useContext() hook in TypeScript, I encountered several errors. Initially, useState is set to an empty array of the interface Phone which is imported. The state and setState are then passed in the value prop. import { createCo ...

Ways to eliminate type errors when coding in TypeScript and prevent them from occurring in the following code

const obj = { extend: (p: { property: string; methods: { name: string; call: string; params: number }[]; }) => { obj[p.property] = {}; p.methods.forEach((m) => { obj[p.property][m.name] = (params: any[]) => m.call ...

I encountered a function overload error while creating a component for the API service

As a developer venturing into API design with a backend server that handles client-side calls, I find myself grappling with Typescript, transitioning from a Java background. Encountering the error message "No overload matches this call" has left me seeking ...

Guide to separating the bytes of a number and placing them into an Uint8Array

I am looking to convert a JavaScript number into the smallest possible uint8array representation. For example : 65 535 = Uint8Array<[255,255]> (0b1111111111111111 = [0b11111111, 0b11111111]) 12 356 = Uint8Array<[48,68]> (0b0011000001000100 = [ ...

Tips on Configuring the Attributes of a TypeScript Object in Angular 2

A TypeScript class called Atom is defined as follows: export class Atom { public text: String; public image: boolean; public equation: boolean; } To create an object of type Atom class and set its properties, the following steps are used: atom: ...

Angular project facing issues during Maven build process

Hi there, I'm currently facing some challenges while trying to deploy my Angular and Spring Boot application. Whenever I run the command mvn clean compile spring-boot:run, I encounter a build failure from my Angular pom file. The error message I am r ...

The values obtained from an HTTP GET request can vary between using Curl and Ionic2/Angular2

When I make a GET request using curl in the following manner: curl https://api.backand.com:443/1/objects/todos?AnonymousToken=my-token I receive the correct data as shown below: {"totalRows":2,"data":[{"__metadata":{"id& ...

How does Typescript handle when an RxJS Observable emits numbers but the observer.next() takes a string?

Imagine having this method within a Typescript/Angular project: subscribeSubject() { const subject = new Subject(); subject.subscribe({ next: (v1: number) => console.log('v1=',v1) }); subject.subscribe({ next: ( ...