gpt4 book ai didi

javascript - Svelte 每个循环都不会更新,而对象是动态更改的

转载 作者:行者123 更新时间:2023-12-04 15:58:30 24 4
gpt4 key购买 nike

当我尝试动态更改每个循环变量的数据时,不会刷新 DOM 元素。我如何重绘每个循环值

<script>
// default messages object
let messages =[{
"name":"John",
"message":"Hi",
"checked": false
},
{
"name":"Anthony",
"message":"welcome",
"checked": true
},
{
"name":"David",
"message":"thank you",
"checked": false
}]
//click function to change the values dynamically
function test(){
messages.map(ob=>{
ob.checked = true;
})
console.log(messages)
}
</script>
模板代码
<div>
{#each messages as m}
<div>{m.checked}
<input type="checkbox" bind:checked={m.checked}>
{m.name}
</div>
{/each}
<br>
<button on:click={()=>{test()}}> check all values</button>
</div>
片段链接: https://svelte.dev/repl/887e7e2de4114ec1bb3625c264b9a62c?version=3.24.1
任何帮助,将不胜感激!谢谢 :)

最佳答案

您不能仅通过在 svelte 中更改其属性来更新数组/对象。

A simple rule of thumb: the name of the updated variable must appear on the left hand side of the assignment.


Updating Arrays and Objects

关于javascript - Svelte 每个循环都不会更新,而对象是动态更改的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63372044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com