问题描述
出于学习目的,我正在Svelte中开发一个小型Todo应用程序(对Svelte来说是新手)。
在App.svelte中,我将TodoItem
数组的todos
组件循环导入:
import TodoItem from './TodoItem.svelte';
//more code
{#each todos as t,index}<TodoItem />{/each}
在TodoItem.svelte
中,我有:
<script>
import { createEventDispatcher } from 'svelte';
export let index;
export let t;
export let id;
export let title;
export let completed;
function deleteTodo(tid){
let itemIdx = todos.findIndex(x => x.id == tid);
todos.splice(itemIdx,1);
todos = todos;
}
function editTodo(tid) {
isEditMode = true;
let itemIdx = todos.findIndex(x => x.id == tid);
currentToDo = todos[itemIdx];
}
function completeTodo(tid){
let itemIdx = todos.findIndex(x => x.id == tid);
let todo = todos[itemIdx];
todo.completed = !todo.completed;
todos = todos;
}
</script>
<tr>
<td>{index + 1}</td>
<td class="{t.completed == true ? 'completed' : ''}">{t.title}</td>
<td class="text-center"><input type="checkbox" checked="{t.completed}" on:change={completeTodo(t.id)}></td>
<td class="text-right">
<div class="btn-group">
<button on:click="{() => editTodo(t.id)}" class="btn btn-sm btn-primary">Edit</button>
<button on:click="{deleteTodo(t.id)}" class="btn btn-sm btn-danger">Delete</button>
</div>
</td>
</tr>
由于我没有弄清原因,我在此组件中遇到了Cannot read property 'title' of undefined
错误,如 REPL 所示。
我在做什么错了?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)