Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1541

how to work with data models in svelte 5 (example: Product, Product[])

$
0
0

in svelte 4, I used the following approach:

<script lang="ts">    class Product {        id: number = 0;        name: string = "";        description: string = "";    }    let products: Product[]=[];    let productForAdd = new Product();</script>}<div><input bind:value={product.name} /><input bind:value={product.description} /><input bind:value={product.image} /><button on:click={() => {        products = [...products, productForAdd];        productForAdd = new Product();}>            Add</button></div>{#each products as product}<div><div>{product.id}</div><div>{product.name}</div><div>{product.description}</div></div>{/each}

How can I do the same in svelte 5?If I use the Product class without runes, then errors about the lack of reactivity appear.If I add the state rune to a class, I cannot get data from an object created based on this class, $state.snapshot(products) turns out to be empty.And if I add a nested array with other objects to the Product class, for example, of this type:

   class Category {        name: string = "";        description: string = "";    }   class Product {        id: number = 0;        name: string = "";        description: string = "";        categoryes: Category[]=[];    }

It turns out to be a complete trash


Viewing all articles
Browse latest Browse all 1541

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>