获取错误"property does not exist"



我修改了我的问题以更具体。 现在我不关心所需的行为,我只需要纠正语法错误

我正在学习本教程,我遇到了此代码中的错误。

严重性:"错误">

消息:"属性'偏移量'在类型'寻呼服务提供程序'上不存在。

实际上,我对这三个变量有相同的错误。

that.pageSize,that.offset,that.size

public async getPager(tableName:string,pageSize: number = 10) {
let pageSize = pageSize;
let offset = 0;
let limit = pageSize;
let size = await this.getTotal(tableName);
let that = this;
return  {
initialPage:function(){
return new Promise((resolve,reject)=>{
var d = [];
that.executeSql(tableName,limit,offset).then((data)=>{
console.log(JSON.stringify(data));
for(var i = 0 ; i < data.rows.length ; i++)
{
d.push(data.rows.item(i));
}
resolve(d);
},(e)=>{
reject(e);
});
});
},
nextPage:function(){
if(that.offset <= that.size - that.pageSize )
{  
that.offset +=  that.pageSize;
}
return new Promise((resolve,reject)=>{
var d = [];
that.executeSql(tableName,limit,offset).then((data)=>{
for(var i = 0 ; i < data.rows.length ; i++)
{
d.push(data.rows.item(i));
}
resolve(d);
},(e)=>{
reject(e);
});
});                    
}            
};}

当你使用关键字function来声明一个函数时,函数的 this 不是指上this。 所以在函数体中使用this,指的是函数本身。

您面临的问题与以下事实有关:您的函数是在已经定义了this的类中声明的,因此您需要一种方法在嵌套函数内部引用上this

class Test {
hello () { console.log('hello') }
method () {
this.hello() // It will work because `this` refers to the class
function sayHello () {
return this.hello()
// it won't work because `this` refers to the function sayHello
}
return sayHello()
}
}

若要绕过此限制,可以在代码位于上限范围内时将上this保存在变量中。此变量通常称为thatself

class Test {
hello () { console.log('hello') }
method () {
var that = this // that is now refering to the class
this.hello() // It will work because `this` refers to the class
function sayHello () {
return that.hello()
// that is still refering to the class so it will succeed
}
return sayHello()
}
}

编辑:

避免使用that的另一个技巧是使用 ES6 箭头功能。在箭头函数中,this始终指上部范围。

class Test {
hello () { console.log('hello') }
method () {
this.hello() // It will work because `this` refers to the class
// `this` refers to the upper scope by default so it works
const sayHello = () => this.hello()
return sayHello()
}
}

编辑2:

您的代码应该是:

public async getPager(tableName: string, pageSize: number = 10) {
let pageSize = pageSize;
let offset = 0;
let limit = pageSize;
let size = await this.getTotal(tableName);
let that = this;
return  {
initialPage: function () {
return new Promise((resolve,reject)=>{
var d = [];
that.executeSql(tableName, limit, offset).then(data => {
console.log(JSON.stringify(data));
for(var i = 0 ; i < data.rows.length ; i++) {
d.push(data.rows.item(i));
}
resolve(d);
}, e => {
reject(e);
});
});
},
nextPage: function () {
if(offset <= size - pageSize ) {  
offset += pageSize;
// no need to use `that` because you used `let`
}
return new Promise((resolve, reject) => {
var d = [];
that.executeSql(tableName, limit, offset).then(data => {
for(var i = 0 ; i < data.rows.length ; i++) {
d.push(data.rows.item(i));
}
resolve(d);
}, e => {
reject(e);
});
});                    
}            
};
}

'that' 只是一个变量的名称,用于在代码开头存储 'this' 的原始值,因为 'this' 的值会发生变化。变量名称可以很容易地是"狗"或"苹果"。

如果您打算在该时间点访问"this"的当前值,则可以选择稍后在代码中使用"this"。否则,您可能会引用存储其值的原始变量,例如"that","dog"或"apple"。

getPager

是一个方法:如果你使用已经丢失的上下文调用它,that将获得当前的this值,该值没有指向正确的上下文。

const someInstance = new SomeClass()
someInstance.getPager() // You call getPager directly from the someInstance context
someHTMLButton.onClick = someInstance.getPager // Here the getPager method lost its context

解决方案是将getPager绑定到someInstance。这样,它将始终具有指向someInstancethis上下文。

someHTMLButton.onClick = someInstance.getPager.bind(someInstance)

相关内容

最新更新