Ionic 2:导致"Can't resolve all parameters"的自定义提供程序



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

字符页

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');
  }
}

尝试使用 forwardRef。图书导航服务似乎正在与某些导入形成循环依赖关系。在字符页面中,

 constructor(public navCtrl: NavController, 
              public navParams: NavParams, 
              public @Inject(forwardRef(() =>BookNavigation)bookNavigation: BookNavigation) {}
我将在这里

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

它发生在我身上是因为我使用的提供程序不是真正正确的,但是提供程序上没有错误。我从组件中删除了提供程序,一切都很好

相关内容

  • 没有找到相关文章

最新更新