[英]Uncaught (in promise) TypeError: this is undefined
When I call deleteCategory in View Class it gives me an error.当我在视图 Class 中调用 deleteCategory 时,它给我一个错误。 The error in calling del(event.target.id) inside deleteCategory.
在 deleteCategory 中调用 del(event.target.id) 的错误。 It worked when I called it this way directly from view Class
当我直接从视图 Class 中以这种方式调用它时,它起作用了
deleteCategory(service){
this.tbody.addEventListener('click',async event => {
const clicked=event.target.tagName.toLowerCase();
if (clicked == 'a'){
const a= await this.service.deleteCategory(service.deleteCategory(event.target.id);
}
})
}
Is there is a way to call the deleteCategory in Controller Class using the event handler inside View Class??有没有办法使用视图 Class 中的事件处理程序调用 Controller Class 中的 deleteCategory?
class View{
constructor(){
this.e=document.createElement.bind(document);
this.root=document.getElementById('root');
this.tbody=document.querySelector('[table-items]');
}
categorylist(lists){
lists.forEach((list) => {
const tr=createTableRow(this.tbody,"");
const td=createTableData(tr,list.name,"pl-4");
const tdEditDelete=createTableData(tr,"","text-right pr-4");
const editButton=createButton(tdEditDelete,'submit','Edit',list.id,'btn btn-primary mr-2');
const deleteButton=createA(tdEditDelete,'Delete',list.id,'btn btn-danger');
this.deleteButton=deleteButton;
})
}
deleteCategory(del){
this.tbody.addEventListener('click', event => {
const clicked=event.target.tagName.toLowerCase();
if (clicked == 'a'){
del(event.target.id);
}
})
}
}
class Controller{
constructor(service,view){
this.view=view;
this.service=service;
this.displayCategoryList();
this.view.deleteCategory(this.deleteCategory);
//this.deleteCategory();
}
async displayCategoryList() {
const lists=await this.service.getLists();
this.lists=lists;
this.view.categorylist(lists);
}
async deleteCategory(id){
//console.log(id);
const a= await this.service.deleteCategory(id);
}
}
I found this solution and worked for me.我找到了这个解决方案并为我工作。
Instead of calling deleteCategory constructor in Controller Class而不是在 Controller Class 中调用 deleteCategory 构造函数
this.view.deleteCategory(this.deleteCategory);
Replaced it with this换成这个
this.view.deleteCategory((async(id)=> {
const a= await this.service.deleteCategory(id);
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.