2017-07-07 69 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

優秀!樂意效勞。 –

相關問題