Javascript 爱奥尼亚2应用程序冲突“;“公众”;vs";“公共静态”;
我是这方面的业余爱好者,试图编写一个应用程序,但我发现代码中有冲突的部分。我没有一个干净的代码,只是为了让事情顺利进行,我试过这样做:Javascript 爱奥尼亚2应用程序冲突“;“公众”;vs";“公共静态”;,javascript,typescript,ionic-framework,ionic2,ionic-native,Javascript,Typescript,Ionic Framework,Ionic2,Ionic Native,我是这方面的业余爱好者,试图编写一个应用程序,但我发现代码中有冲突的部分。我没有一个干净的代码,只是为了让事情顺利进行,我试过这样做: constructor(platform: Platform, public nativeStorage: NativeStorage) { platform.ready().then(() => { this.nativeStorage.getItem('NoTaleDB').then(function (json) {
constructor(platform: Platform, public nativeStorage: NativeStorage) {
platform.ready().then(() => {
this.nativeStorage.getItem('NoTaleDB').then(function (json) {
if (json) {
GlobalVariables.novelList = JSON.parse(json);
}
});
});
}
public static save() {
this.nativeStorage.setItem('NoTaleDB', JSON.stringify(GlobalVariables.novelList)).then(() => {}, () => {});
}
得到了这个错误:
Property 'nativeStorage' does not exist on type 'typeof StorageService'
当我将函数修改为以下内容时:
public save() {
this.nativeStorage.setItem('NoTaleDB', JSON.stringify(GlobalVariables.novelList)).then(() => {}, () => {});
}
它会找到nativeStorage,但我从页面和服务本身中得到了这个错误:
Property 'save' does not exist on type 'typeof StorageService'.
我已经尝试完成这个应用程序很长一段时间了,但最后只尝试修复bug。请提供一个简单的解决方案,一个新手可以理解的。谢谢 假设您希望此函数的函数体起作用:
public static save() {
this.nativeStorage.setItem('NoTaleDB', JSON.stringify(GlobalVariables.novelList)).then(() => {}, () => {})
}
此
对于静态成员不可用,因为静态成员退出类而不是实例(此引用的对象)
固定代码
必须是:
public save() {
this.nativeStorage.setItem('NoTaleDB', JSON.stringify(GlobalVariables.novelList)).then(() => {}, () => {})
}
现在您将看到错误:
类型“typeof StorageService”上不存在属性“save”
您正在呼叫StorageService.save
。这是错误的。您应该在实例上调用save
,例如
new StoargeService(pass in the stuff it needs).save(); // Works now
更多
TypeScript类上的一些文档:假设您希望此函数的函数体起作用:
public static save() {
this.nativeStorage.setItem('NoTaleDB', JSON.stringify(GlobalVariables.novelList)).then(() => {}, () => {})
}
此
对于静态成员不可用,因为静态成员退出类而不是实例(此引用的对象)
固定代码
必须是:
public save() {
this.nativeStorage.setItem('NoTaleDB', JSON.stringify(GlobalVariables.novelList)).then(() => {}, () => {})
}
现在您将看到错误:
类型“typeof StorageService”上不存在属性“save”
您正在呼叫StorageService.save
。这是错误的。您应该在实例上调用save
,例如
new StoargeService(pass in the stuff it needs).save(); // Works now
更多
关于TypeScript类的一些文档:谢谢。您有代码
L15:new-StorageService.save()
。我有一个新的StoargeService(传入它需要的东西)。save()。不一样。我发布的代码是正确的,谢谢。您有代码L15:new-StorageService.save()
。我有一个新的StoargeService(传入它需要的东西)。save()。不一样。我发布的代码是正确的