如果我使用箭头函数,我可以路由到另一个组件,但我无法获取数据表值,下面是代码:
$('#list-country tbody').on('click', '.fa-edit', function() {
var tr = $(this).closest("tr");
var data = $("#list-country").DataTable().row(tr).data();
console.log(data);
this.router.navigate(['/apps/edit-country']);
this.routePage();
});
如果我使用普通函数,我可以获取值,但我无法路由,下面是代码:
$('#list-country tbody').on('click', '.fa-edit', () => {
var tr = $(this).closest("tr");
var data = $("#list-country").DataTable().row(tr).data();
console.log(data);
this.routePage();
});
这些函数在 ngInit()
中。
最佳答案
此解决方案可能对您有用:
- 首先在全局范围内声明一个变量(方法/类)
- 在构造函数中将它初始化为
this
(如果你有的话) 使用
self
代替this
如下所示var self = this; //declare this in constructor or method $('#list-country tbody').on( 'click', '.fa-edit', function () { var tr = $(this).closest("tr"); var data = $("#list-country").DataTable().row(tr).data(); console.log(data); self.router.navigate(['/apps/edit-country']); self.routePage(); });
关于javascript - 如何获取数据表行值并路由到另一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44801732/