Using Vue: how to iterate over properties of an external class with v-for

I am attempting to create a v-for loop using properties from another class in order to utilize it across multiple components.

The HTML code for Now.vue:

<tr v-for="(el, index) in rendering.elements" :key="index">
    <td>{{index + 1}}</td>
    <td>{{el.name}}</td>
</tr>

The TypeScript code for Now.vue:

    @Component
    export default class Now extends Vue {
        @Prop() private rendering: Render = new Render();

        /**
         * On vue created
         */
        public created () : void
        {
            ApiClient.executeRequest('GET', '/api/stock/state')
                .subscribe((response: any): void => {
                    this.rendering.elements = response.data.result;
                })
        }
    }

Although the rendering.elements are correctly set with the values, the v-for loop does not update.

This code snippet resolves the issue:

<tr v-for="(el, index) in elements" :key="index">
    <td>{{index + 1}}</td>
    <td>{{el.name}}</td>
</tr>

    @Component
    export default class Now extends Vue {
        @Prop() private elements: Array<any> = [];

        /**
         * On vue created
         */
        public created () : void
        {
            ApiClient.executeRequest('GET', '/api/stock/state')
                .subscribe((response: any): void => {
                    this.elements = response.data.result;
                })
        }
    }

Why is this happening?

Answer №1

Try implementing Vue.set:

private rendering: Render = new Render()

public created (): void {
    ...
    Vue.set(this.rendering, 'items', response.data.list)
    ...
}

Noted by @RoyJ, this issue is due to Vue's reactivity limitations. The component must have the object and its properties already set up; using set helps make them observable.

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

Retrieving data from a slot's component in Vue.js

I am having trouble retrieving data from the component using slots. Here is the code I am working with: codepen.io/anon/pen/BbOEZz I am trying to get the line: <p slot="desc">{{ desc_01 }}</p> to display the value along with the date in the t ...

Ways to retrieve particular items/properties from the JSON response string

Is there a way to extract and display only the first 3 items from my JSON data instead of the entire string? Below is the code I am currently using to loop through and display my records: <tr v-for="(list) in myData" v-bind:key="list.ema ...

Dynamically showcase dropdown menus within a single component

I am looking to implement multiple dropdowns in a single component, using one variable to control their display and also have them close when clicking away from the dropdown. Here is my code snippet: <div class="dropdown"> <button @cli ...

Arranging Data in AngularJS based on Selected Filter Criteria

I have a filter function that currently only returns the name values in my table. I would like to add options for both ascending and descending sorting to this filter. Progress So Far: I am able to retrieve values from all inputs, including the name and ...

What methods can we employ to ensure that Typescript adheres to the default generic argument type in this situation?

const EventKeys = { openItem: 'openItem', changeActiveItem: 'changeActiveItem', selectionToggled: 'selectionToggled', } as const type EventKeys = keyof typeof EventKeys class Test<Evmt>{ subscribe<Curren ...

On Vue.js, what is the optimal location for deploying "app initialization" code?

My main concern is about executing code before the app starts running. I am currently using vuex, and my priority is to dispatch a getUser action as the initial step, regardless of the user's current route, in order to fetch user details from the API ...

Include a new route in the Vue.js router dynamically during runtime

I am in the process of developing an application and I want to incorporate extensions into it. These extensions will have their own Vue components, views, and routes. Instead of rebuilding the entire app, I am looking for a way to dynamically add these new ...

During the printing process, Vuetify applications allocate space for hidden elements

I have developed a Vue.JS and Vuetify application inspired by the Google Contacts layout. The design includes both a toolbar and a navigation drawer. My goal is to print the content from the browser without including the toolbar and nav drawer. To achieve ...

Make sure to verify if all values are contained within an array by utilizing JavaScript or TypeScript

These are the two arrays I'm working with. My goal is to ensure that every value in ValuesToBeCheckArr is present in ActualArr. If any values are missing from ActualArr, the function should return 0 or false. Additionally, there is an operator variabl ...

What is the standard method for setting universal variables in Vue.js?

As I work on developing Vue applications with single file components, I have noticed that many of my components require common configuration information. For instance, I often need to define an object containing various delivery methods like this: const De ...

Error: The module 'fs' could not be located after running Rollup

Having encountered this issue, I understand that it has been a common question on the internet. I apologize for potentially duplicating the query. Despite trying various solutions found online, none have proven to be effective. The Problem: The problem ar ...

When using @testing-library/react (rtl), the 'waitFor' function achieves success even without the need for the 'await' keyword

waitFor() is causing my test to fail while waitFor() (without await) makes it pass. The official documentation states: Async methods return a Promise, so you must always use await or .then(done) when calling them. (https://testing-library.com/docs/guide ...

Converting an Observable containing an Array of StaffInterface objects to a plain Array of StaffInterface objects in @ngrx/store select

Trying to retrieve an Array<StaffInterface> from an Observable<Array<StaffInterface>> in ngrx store.select. The store.select method returns the Observable<Array<StaffInterface>>, which I then need to convert into an Array<S ...

Record audio without relying on Mediarecorder

I am combining two AudioBuffers to form a single one. I believe this can be done without using the MediaRecorder, as real-time recording is not required. This is my current approach: concatenateAudio(buffers: AudioBuffer[]): void { const totalLength ...

Getting props value in parent component using Vue JS

In my development project, I am working with three key components: component-1, component-2, and an App component. Initially, I pass a Boolean prop from component-1 to component-2. Through the use of a @click event, I am able to toggle this prop value betw ...

What is the best way to calculate checksum and convert it to a 64-bit value using Javascript for handling extremely large files to avoid RAM overflow?

Question: What is the best method for generating a unique and consistent checksum across all browsers? Additionally, how can a SHA256/MD5 checksum string be converted to 64-bit? How can files be read without requiring excessive amounts of RAM when ...

I am experiencing an issue where the Axios configuration does not display the OnUploadProgress on my response

I have been attempting to track the progress of file uploads from the front end, but I am encountering an issue where the onUploadProgress is not being received in the configuration catch. This problem arises as I am relatively new to using Axios. axios({ ...

Error: Attempting to access an undefined property ('call') on Next.js is causing a TypeError

Exploring the realms of Next.js and Typescript for a new project! My goal is to utilize next.js with typescript and tailwind CSS by simply entering this command: npx create-next-app -e with-tailwindcss my-project Smooth sailing until I hit a snag trying t ...

Top method for changing Enum to Set in TypeScript

Take a look at the enum below: enum Animes { OnePiece = 'One Piece', Naruto = 'Naruto', Bleach = 'Bleach' } How can we efficiently transform this enum into a Set? ...

Error TS2322 occurs during compilation in Typescript when using ng.IPromise

Having some issues while using Angular 1.x with Typescript. Here is the code causing trouble: get(id): ng.IPromise<Server.MyItem> { return this.$http.get(`${this.baseAddress}/${id}`).then(d => d.data); } After compiling with tsc, I am encoun ...