gpt4 book ai didi

javascript - 当我的 props 发生变化时,如何在 Svelte 中强制重新渲染?

转载 作者:行者123 更新时间:2023-12-02 22:41:32 28 4
gpt4 key购买 nike

我有一个总计组件,需要对多维数组中的多个数组的相同索引进行总计,类似于电子表格中的总计列。我有一个不同的组件,它允许我更改数组中的值,并且还可以对电子表格中的行进行总计。我在更改数组后重新渲染总计组件和/或更新列总计时遇到麻烦。我认为我可能需要在总计组件中进行响应式(Reactive)声明,但这似乎不起作用。

这里是 REPL. 的链接

这是代码:

Totals.svelte

<script>
export let jobs

//this creates a new array which is two indexes long for each of the columns
//then fills each array with the sum of column
//HOW DO I GET THIS TO UPDATE EACH TIME A JOB HOURS ENTRY IS CHANGED?
$: totals = new Array(2).fill(null).map((v, i) => {
let total = 0;
jobs.jobHours.forEach((v) => {
total += Number(v[i]);
});
return total;
});

//this sums the total hours row to give the grand total
//HOW DO I GET THIS TO UPDATE EACH TIME THE TOTALS VARIABLE IS CHANGED?
$: grandTotal = totals.reduce((acc, v) => (acc += v));

</script>

<style>
container {
display: grid;
grid-template-columns: 2fr 1fr 1fr 1fr;

}
div {
width: 100%;
height: 100%;
width: 150px;
}
</style>

<container>
<div>Total Hours</div>
{#each totals as total}
<div>
{total}
</div>
{/each}
<div>{grandTotal}</div>
</container>
App.svelte

<script>
import JobEntries from './JobEntries.svelte';
import Totals from './Totals.svelte';

const jobs = {
jobNames: ['Job1', 'Job2', 'Job3'],
jobHours: Array.from(Array(3), () => Array.from(Array(2), ()=>1))
};
</script>

<JobEntries {jobs}/>
<Totals {jobs} />

JobEntries.svelte

<script>
export let jobs
let cell = Array.from(Array(3), () => Array.from(Array(3)));
</script>

<style>
container {
display: grid;
grid-template-columns: 2fr 1fr 1fr 1fr;

}
input, div {
width: 100%;
height: 100%;
min-width: 150px;
}
</style>

<container>
<div>
Name
</div>
<div>
Time 1
</div>
<div>
Time 2
</div>
<div>
Total
</div>
{#each jobs.jobNames as name, i}
<input
type="text"
bind:this={cell[i][0]}
bind:value={name} />
{#each jobs.jobHours[i] as hour, j}
<input
type="number"
bind:this={cell[i][j+1]}
bind:value={hour}
/>
{/each}
<div>
{jobs.jobHours[i].reduce((acc,v)=>acc+=v)}
</div>
{/each}
</container>

最佳答案

在父组件中绑定(bind) prop,允许子组件更改 prop。

App.svelte

<script>
import JobEntries from './JobEntries.svelte';
import Totals from './Totals.svelte';
let jobs = {
jobNames: ['Job1', 'Job2', 'Job3'],
jobHours: Array.from(Array(3), () => Array.from(Array(2), ()=>1))
};
</script>

<JobEntries bind:jobs/>
<Totals {jobs} />

JobEntries.svelte

<script>
export let jobs
let cell = Array.from(Array(3), () => Array.from(Array(3)));

</script>

<style>
container {
display: grid;
grid-template-columns: 2fr 1fr 1fr 1fr;

}
input, div {
width: 100%;
height: 100%;
min-width: 150px;
}
</style>

<container>
<div>
Name
</div>
<div>
Time 1
</div>
<div>
Time 2
</div>
<div>
Total
</div>
{#each jobs.jobNames as name, i}
<input
type="text"
bind:this={cell[i][0]}
bind:value={name} />
{#each jobs.jobHours[i] as hour, j}
<input
type="number"
bind:this={cell[i][j+1]}
bind:value={hour}
/>
{/each}
<div>
{jobs.jobHours[i].reduce((acc,v)=>acc+=v)}
</div>
{/each}
</container>

关于javascript - 当我的 props 发生变化时,如何在 Svelte 中强制重新渲染?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58567820/

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