我有一个带有angular2应用程序的nativescript for android,我想利用nativescript-sqlite的离线存储功能。问题是我得到以下异常:
Failed to find module: "nativescript-sqlite", relative to: /app/tns_modules/
在下面一行:
var Sqlite = require("nativescript-sqlite");
我使用以下命令安装插件
tns插件添加nativescript-sqlite
我创建了一个带有角度服务的db.service.ts文件,其内容如下:
import {Injectable} from "@angular/core";
import {Config} from "../config";
import {Observable} from "rxjs/Rx";
import "rxjs/add/operator/do";
import "rxjs/add/operator/map";
import {Profile} from "../profile/profile";
var Sqlite = require("nativescript-sqlite");
@Injectable()
export class DbService {
database: any;
constructor() {
(new Sqlite("gtel.db")).then(db => {
this.database = db;
db.resultType(Sqlite.RESULTSASOBJECT);
this.database.execSQL("CREATE TABLE IF NOT EXISTS profile (id INTEGER PRIMARY KEY AUTOINCREMENT," +
" username TEXT, idnumber TEXT, firstname TEXT, lastname TEXT, mobilenumber TEXT, emailaddress TEXT)").then(id => {
console.log("created table profile")
}, error => {
console.log("created table profile error", error);
});
}, error => {
console.log("OPEN DB ERROR", error);
});
}
createProfile(profile: Profile){
return this.database.execSQL("INSERT INTO profile(username, idnumber, firstname, lastname, mobilenumber, emailaddress) VALUES (?, ?, ?, ?, ?, ?)",
[profile.username, profile.idNumber, profile.firstName, profile.lastName, profile.mobileNumber, profile.emailAddress]);
}
getProfile(id: number){
return this.database.get('select * from Hello where id=?', [id])
}
handleErrors(error: Response) {
console.log(JSON.stringify(error.json()));
return Observable.throw(error);
}
}
非常感谢您的帮助。
答案 0 :(得分:1)
大多数这些模块错误都可以通过删除平台然后再添加来解决。
Android适用于:
tns platform remove android
tns platform add android
和iOS:
tns platform remove ios
tns platform add ios
您可以在评论中看到此问题已得到解决。只是回答这个,所以每个人都可以看到它