离子2:自定义提供商导致"无法解决所有参数"

时间:2017-03-20 21:12:55

标签: angular typescript ionic2

我已经创建了一个测试提供程序,我试图将它注入两个页面以便我可以共享数据和方法,但是当我将提供程序添加到页面构造函数时,我收到一个错误,说& #34;无法解析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');
  }

}

3 个答案:

答案 0 :(得分:1)

尝试使用forwardRef。 BookNavigation Service似乎与一些导入形成了循环依赖。 在CharacterPage中,

 constructor(public navCtrl: NavController, 
              public navParams: NavParams, 
              public @Inject(forwardRef(() =>BookNavigation)bookNavigation: BookNavigation) {}

答案 1 :(得分:1)

我离开了我在这里发现的东西。显然,将服务用于导航目的并不是一个好主意(请参阅此处https://forum.ionicframework.com/t/whats-the-best-practice-if-you-want-to-control-the-navigation-from-a-service/52616/2)。它会导致一系列导致其他问题的错误,所以我决定放弃这个想法。 我仍然不确定如何绕过这个问题并只有一个文件,我可以设置我的所有数据和方法保持其余的代码清洁。我已经读过它可能通过事件发送者的最佳方式,但我仍然不了解它们以确定

答案 2 :(得分:0)

发生在我身上的原因是我使用的供应商并不正确,但是该供应商没有任何错误。我从组件中删除了提供程序,一切都很好