我是这个东西的业余爱好者,尝试编写应用程序,但我发现我的代码上有冲突的部分。我没有干净的代码只是让事情工作,我像这样尝试过:
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(() => {}, () => {});
}
它找到了本机存储,但我从页面和服务本身收到此错误:
Property 'save' does not exist on type 'typeof StorageService'.
我已经尝试完成这个应用程序很长时间了,但最终只是尝试修复错误。请提供一个简单的解决方案,新手可以理解。谢谢。<3
假设您希望其函数体正常运行:
public static save() {
this.nativeStorage.setItem('NoTaleDB', JSON.stringify(GlobalVariables.novelList)).then(() => {}, () => {})
}
this
不适用于静态成员,因为静态成员退出类而不是实例(this
引用的东西(。
固定代码
必须是 :
public save() {
this.nativeStorage.setItem('NoTaleDB', JSON.stringify(GlobalVariables.novelList)).then(() => {}, () => {})
}
现在您收到错误:
属性"save"在类型"存储服务"上不存在。
你打电话给StorageService.save
.这是错误的。您应该在实例上调用save
,例如
new StoargeService(pass in the stuff it needs).save(); // Works now
更多
关于打字稿类的一些文档:https://basarat.gitbooks.io/typescript/content/docs/classes.html