2017-07-07 80 views
2

我的家庭组件具有对我的主要列表项目进行操作的功能。我试图通过称为item-details的另一个组件访问这些函数。导入组件时,Ionic 2“无法解析所有参数”

但是,当我导入HomePage组件,并将其添加到item-details.ts的构造函数时,出现以下错误:“运行时错误无法解析ItemDetailPage的所有参数:[[object Object], [object object],?)“。 error image

项目-details.ts:

import { Component } from '@angular/core'; 
import { NavParams, NavController } from 'ionic-angular'; 
import { HomePage } from '../home/home'; 

@Component({ 
    selector: 'page-item-detail', 
    templateUrl: 'item-detail.html' 
}) 
export class ItemDetailPage { 
    title; 
    description; 

    constructor(
    public navParams: NavParams, 
    public navController: NavController, 
    public home: HomePage 
){ 

    } 

    ionViewDidLoad() { 
    this.title = this.navParams.get('item').title; 
    this.description = this.navParams.get('item').description; 
    } 

    deleteItem(item){ 
    //call deleteItem in home.ts 
    } 
} 

home.ts:

import { Component } from '@angular/core'; 
import { ModalController, NavController, ViewController } from 'ionic-angular'; 
import { AddItemPage } from '../add-item/add-item' 
import { ItemDetailPage } from '../item-detail/item-detail'; 
import { Data } from '../../providers/data/data'; 

@Component({ 
    selector: 'page-home', 
    templateUrl: 'home.html' 
}) 
export class HomePage { 

    public items = []; 

    constructor(
    public navCtrl: NavController, 
    public modalCtrl: ModalController, 
    public dataService: Data 
    ) { 
    this.dataService.getData().then((todos) => { 
     if(todos){ 
     this.items = JSON.parse(todos); 
     } 
    }); 
    } 

    ionViewDidLoad(){ 

    } 

    addItem(){ 
    let addModal = this.modalCtrl.create(AddItemPage); 

    addModal.onDidDismiss((item) => { 
      if(item){ 
      this.saveItem(item); 
      } 
    }); 
    addModal.present(); 
    } 

    saveItem(item){ 
    this.items.push(item); 
    this.dataService.save(this.items); 
    } 

    viewItem(item){ 
    this.navCtrl.push(ItemDetailPage, { 
     item: item 
    }); 
    } 

    deleteItem(item){ 
    //code to delete an item from items array 
    } 
} 

这里是我的文件结构的情况下画面它是相关的。 file structure

任何人都可以帮我弄清楚什么是错的,以及如何解决它?

+0

你可以尝试在plnkr中重新创建问题,没有任何东西是不正确的。 http://embed.plnkr.co/SJ8GtqbRntby5yGzLEft/ –

回答

1

在你ItemDetailPage组件,你问的容器来解决HomePage组件时,你应该真的来要求一个服务,而不是。

如上图所示,在ItemDetailPage,你试图去一个HomePage组件的引用(的创建,也是ItemDetailPage参照组件),这使得一个循环引用。这是行不通的。

当你有一个服务可以满足你的需要时,真的不需要解决一个组件。这是服务的目的,共享功能。将您的物品管理代码(saveItem,addItem,deleteItem)移出到HomeItemDetail可以分别引用和使用的服务中。

我希望这对你有帮助。

+0

谢谢。这是一个完美的答案。我把所有东西都搬到了一个新的提供者中,把提供者导入到了我的其他组件中,现在一切都完美了! – Ocen

+0

优秀!乐意效劳。 –

相关问题