【发布时间】:2019-08-04 16:46:05
【问题描述】:
我正在尝试为我的待办事项列表应用程序实现删除方法。我遇到了一个问题,它告诉我在 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,
});
}
【问题讨论】:
标签: javascript reactjs flux