问题描述
我已经创建了一个测试提供程序,我正在尝试将其注入两个页面以便我可以共享数据和方法,但是当我将提供程序添加到页面构造函数时,我收到错误,说无法解析CharacterPage的所有参数:(NavController,NavParams,?)。
I've created a test provider and I'm trying to inject it into two pages so I can share data and methods, but when I add the provider to the page constructor I get an error, saying "Can't resolve all parameters for CharacterPage: (NavController, NavParams, ?)".
Ionic Framework: 2.0.1
Ionic Native: 2.4.1
Ionic App Scripts: 1.1.0
Angular Core: 2.2.1
Angular Compiler CLI: 2.2.1
Node: 6.9.4
OS Platform: Windows 10
Navigator Platform: Win32
app.module.ts
import { NgModule, ErrorHandler } from '@angular/core';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { Storage } from '@ionic/storage';
import { MyApp } from './app.component';
import { BookNavigation } from '../providers/book-navigation';
@NgModule({
declarations: [ //STUFF ],
imports: [ IonicModule.forRoot(MyApp) ],
bootstrap: [IonicApp],
entryComponents: [ //STUFF ],
providers: [
{
provide: ErrorHandler,
useClass: IonicErrorHandler,
},
Storage,
BookNavigation,
]
})
export class AppModule {}
app.components.ts
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar, Splashscreen } from 'ionic-native';
import { BookNavigation } from '../providers/book-navigation';
import { HomePage } from '../pages/home/home';
@Component({
templateUrl: 'app.html',
providers: [BookNavigation]
})
export class MyApp {
rootPage = HomePage;
constructor(platform: Platform) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
Splashscreen.hide();
});
}
characterPage.ts
import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { BookNavigation } from '../../providers/book-navigation';
@Component({
selector: 'page-character',
templateUrl: 'page.html'
})
export class CharacterPage {
constructor(public navCtrl: NavController,
public navParams: NavParams,
public bookNavigation: BookNavigation) {}
}
服务提供商
import { Injectable } from '@angular/core';
import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { Storage } from '@ionic/storage';
@Injectable()
export class BookNavigation {
constructor(public navCtrl: NavController, public navParams: NavParams, public storage:Storage) {
console.log('Hello BookNavigation Provider');
}
}
推荐答案
我要离开我在这里发现的东西。显然,将服务用于导航目的并不是一个好主意(请参阅此处)。它会导致一系列导致其他问题的错误,所以我决定放弃这个想法。
我仍然不确定如何绕过这个问题,只有一个文件,我可以设置我的所有数据和方法保持其余的代码清洁。我已经读过它可能通过事件发送器的最好方式,但我仍然不太了解它们确定
I'm leaving what I found out here. Apparently it's not a good idea to use a service for navigation purposes (see here https://forum.ionicframework.com/t/whats-the-best-practice-if-you-want-to-control-the-navigation-from-a-service/52616/2). It causes a series of errors that lead to other problems so I decided just to abandon the idea.I'm still unsure of how to bypass this problem and have just one file where I can set all my data and methods keeping the rest of the code clean. I've read that the best way it's probably trough events emmitters but I still don't know enough about them to be sure
这篇关于离子2:自定义提供者导致“无法解析所有参数”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!