收到错误 "property does not exist"
getting error "property does not exist"
我修改了我的问题以使其更具体。现在我不关心期望的行为,我只需要更正语法错误
我正在研究 this tutorial 我遇到了这段代码中的错误。
严重性:'Error'
消息:'属性 'offset' 在类型 'PagerserviceProvider' 上不存在。'
实际上我对这三个变量有同样的错误。
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);
});
});
}
};}
'that' 只是用于在代码开头存储 'this' 原始值的变量名称,随着 'this' 值的变化。变量名可以是 'dog' 或 'apple'.
如果您打算在那个时间点访问 'this' 的当前值,您可以稍后在您的代码中选择使用 'this'。否则,您可能会引用存储其值的原始变量,例如'that'、'dog' 或 'apple'。
当你使用关键字function
声明一个函数时,函数的this不引用上层this
。所以在函数体中使用this
,指的是函数本身。
您面临的问题与您的函数是在已经定义了 this
的 class 中声明的事实有关,因此您需要一种方法来引用上面的 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
保存在变量中。此变量通常称为 that
或 self
.
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);
});
});
}
};
}
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
。这样它的上下文 this
将始终指向 someInstance
.
someHTMLButton.onClick = someInstance.getPager.bind(someInstance)
我修改了我的问题以使其更具体。现在我不关心期望的行为,我只需要更正语法错误
我正在研究 this tutorial 我遇到了这段代码中的错误。
严重性:'Error'
消息:'属性 'offset' 在类型 'PagerserviceProvider' 上不存在。'
实际上我对这三个变量有同样的错误。
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);
});
});
}
};}
'that' 只是用于在代码开头存储 'this' 原始值的变量名称,随着 'this' 值的变化。变量名可以是 'dog' 或 'apple'.
如果您打算在那个时间点访问 'this' 的当前值,您可以稍后在您的代码中选择使用 'this'。否则,您可能会引用存储其值的原始变量,例如'that'、'dog' 或 'apple'。
当你使用关键字function
声明一个函数时,函数的this不引用上层this
。所以在函数体中使用this
,指的是函数本身。
您面临的问题与您的函数是在已经定义了 this
的 class 中声明的事实有关,因此您需要一种方法来引用上面的 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
保存在变量中。此变量通常称为 that
或 self
.
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);
});
});
}
};
}
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
。这样它的上下文 this
将始终指向 someInstance
.
someHTMLButton.onClick = someInstance.getPager.bind(someInstance)