我正在尝试为待办事项列表应用程序实现删除方法。我遇到一个问题,它告诉我TodoStore.js中的“ this.deleteTodo不是函数”。我还没有做太多尝试,因为我不知道问题出在哪里。这里可能是什么问题?
我的TodoStore.js
import { EventEmitter } from "events";
import dispatcher from "../dispatcher";
class TodoStore extends EventEmitter {
constructor() {
super()
this.todos = [
{
id: 113464613,
text: "Go Shopping"
},
{
id: 235684679,
text: "Pay Water Bill"
},
];
}
createTodo(text) {
const id = Date.now();
this.todos.push({
id,
text,
});
this.emit("change");
}
getAll() {
return this.todos;
}
handleActions(action) {
switch(action.type) {
case "CREATE_TODO": {
this.createTodo(action.text);
break;
}
case "DELETE_TODO": {
this.deleteTodo(action.id);
break;
}
}
}
}
const todoStore = new TodoStore;
dispatcher.register(todoStore.handleActions.bind(todoStore));
window.dispatcher = dispatcher
export default todoStore;
我的TodoActions.js
import dispatcher from "../dispatcher";
export function createTodo(text) {
dispatcher.dispatch({
type: "CREATE_TODO",
text,
});
}
export function deleteTodo(id) {
dispatcher.dispatch({
type: "DELETE_TODO",
id,
});
}
答案 0 :(得分:0)
您已将INSERT INTO `customers`
(`customerID`, `firstName`, `lastName`, `address`) VALUES
('1', 'moshe', 'rubin', 'haifa'),
('2', 'tanya', 'shelomayev', 'netanya'),
('3', 'itamar', 'ofer', 'haifa');
定义为createTodo
类的方法,因此在通过TodoStore
访问时可以使用,但是您尚未定义this
方法作为deleteTodo
类的成员,因此将无法通过调用指向TodoStore
类实例的deleteTodo
来访问this
方法。