我一直在尝试在多个页面上使用服务,但它适用于 2 个页面中的 1 个页面。这是我的服务:
import {Component} from '@angular/core'
import { DataService } from '../../db/db'
@Injectable()
export class DataService {
constructor(@Inject(SQLite) public db = new SQLite(), public nav: NavController) {
}
这是我使用它的尝试:在这里它工作:
@Component({
templateUrl: 'home.html',
providers: [DataService]
})
export class HomePage {
constructor(public data: DataService){
this.data = data;
this.data.results;
}
}
那里没有:
import { Component, Inject } from '@angular/core'
import { DataService } from '../../../db/db'
@Component({
templateUrl: 'infos.html',
providers: [DataService]
})
export class NewEnigme {
constructor(@Inject(DataService) public data: DataService){
this.data = data;
}
}
并告诉我
"无法解析NewEnigme的所有参数:(?
这是我的应用程序模块:
@NgModule({
declarations: [
MyApp,
CameraPage,
DocPage,
MapPage,
TabsPage,
ScannerPage,
HomePage,
NewEnigme,
TabsNewEnigme,
detailPic
],
imports: [
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
CameraPage,
DocPage,
MapPage,
TabsPage,
ScannerPage,
TabsNewEnigme,
detailPic,
HomePage,
NewEnigme
],
providers: [{provide: ErrorHandler, useClass: IonicErrorHandler}, ConnectivityService, DataService, SQLite]
})
export class AppModule {}
我已经尝试了我在互联网上能找到的所有东西,但我在这里很迷茫。我是新手,我希望得到一些帮助:)
编辑更多细节(取自答案)
如果我在我的数据服务中添加以下函数之一:
launchEnigme(par){
this.nav.push(TabsPage,{
firstPassed: par.nom,
});
}
newEnigme(){
this.nav.setRoot(TabsNewEnigme, {});
}
发生错误。
无法解析 InfoEnigme 的所有参数:(?)。
完整文件 :
import {NavController} from 'ionic-angular'
import {Inject, Injectable} from '@angular/core'
import {SQLite} from 'ionic-native';
import { TabsPage } from '../pages/tabs/tabs'
import { TabsNewEnigme } from '../new/tabs/newTabs'
@Injectable()
export class DataService {
public results: Array<Object>;
constructor(@Inject(SQLite) public db = new SQLite(), public nav: NavController) {
console.log("leqdl501");
db.openDatabase({
name: 'database.db',
location: 'default',
androidDatabaseImplementation: 2// the location field is required
}).then(() => {
db.executeSql("drop table if exists Enigmes", {});
db.executeSql("create table Enigmes(nom VARCHAR(32), description VARCHAR(128))", {});
db.executeSql("INSERT INTO Enigmes (nom, description) VALUES ('Enquete dans Rouen', 'enquete dans Rouen')", {});
db.executeSql("INSERT INTO Enigmes (nom, description) VALUES ('Enquete dans Nantes', 'enquete dans Nantes')", {});
db.executeSql("INSERT INTO Enigmes (nom, description) VALUES ('Enquete dans Paris', 'enquete Parisienne')"
, {}).then(() => {
console.log('CREATED');
this.refresh();
}, (err) => {
console.error('Unable to execute sql: ', err);
});
}, (err) => {
console.error('Unable to open database: ', err);
});
}
refresh() {
this.db.executeSql("SELECT * FROM Enigmes", {}).then((data) => {
this.results = [];
if (data.rows.length > 0) {
for (var i = 0; i < data.rows.length; i++) {
this.results.push({
nom: data.rows.item(i).nom,
description: data.rows.item(i).description
});
}
}
}, (error) => {
console.log("ERROR: " + JSON.stringify(error));
});
}
launchEnigme(par){
this.nav.push(TabsPage,{
firstPassed: par.nom,
});
}
newEnigme(){
this.nav.setRoot(TabsNewEnigme, {});
}
}
似乎您的数据服务和页面之间有一个循环引用,阻止它们正确实例化。
此示例具有循环引用,不起作用
https://plnkr.co/edit/HLsphY5ZswHCIAOHcCJr
import {Component, Injectable} from '@angular/core';
import {NavController} from 'ionic-angular';
import {AlternativePage} from '../../alternative.page';
@Injectable()
export class DataService {
public data = 'Data from service';
constructor(private nav: NavController) { }
public serviceNavigate() {
this.nav.push(AlternativePage);
}
}
如果没有循环引用,您的代码应该可以工作 - 尝试为您的逻辑找到替代解决方案。
https://plnkr.co/edit/sdLPz90fTB2jEMGAiILQ?p=preview