我一直试图在多个页面上使用服务,但它可以在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的所有参数:(?)。'
这是我的app.module.ts:
@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 {}
我已经尝试过在互联网上找到的所有东西,而且我在这里迷失了。 我对此很陌生,我很感激一些帮助:)
如果我在我的DataService中添加其中一个函数:
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, {});
}
}
答案 0 :(得分:1)
您的DataService和Page之间似乎有一个循环引用,阻止它们正确实例化。
此示例具有循环引用,不起作用
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);
}
}
如果没有循环引用,您的代码应该可以运行 - 尝试为您的逻辑找到替代解决方案。