我正在使用離子選項卡。從數據庫中產生的某些選項卡(包括那些沒有圖標)離子2:添加新的動態選項卡後刷新選項卡視圖
現在,當我添加一個新的標籤,並刷新陣列我應該得到3個動態標籤。相反,我有5個(前兩個和前兩個與最新創建的選項卡)儘管陣列正確地有3個對象。
[對象,對象,對象]
所以這裏的相關代碼(標籤組件有聽標籤創建一個事件):
// tabs.ts
import { Component } from '@angular/core';
import { Events } from 'ionic-angular';
import { DatabaseService } from "../../providers/database.service";
import { ItemsPage } from '../items/items';
import { ContactPage } from '../contact/contact';
import { HomePage } from '../home/home';
import { CategoryPage } from '../category/category';
@Component({
templateUrl: 'tabs.html'
})
export class TabsPage {
categories: any = [];
tab1Root = HomePage;
tab2Root = CategoryPage;
tab3Root = ItemsPage;
tab4Root = ContactPage;
constructor(public databaseService: DatabaseService, public events: Events) {
this.events.subscribe('category:created',() => {
this.refreshTabs();
});
}
ngOnInit() {
this.refreshTabs();
}
refreshTabs() {
this.databaseService.getCategories().then(categories => {
this.categories = categories;
console.log(this.categories); // [Object, Object, Object]
});
}
}
因此,無論何時我添加或編輯標籤我呼籲:
this.events.publish('category:created');
tabs.html:
<ion-tab [root]="tab2Root" tabTitle="{{ category.name }}" [rootParams]="category.id" *ngFor="let category of categories"></ion-tab>
任何想法,爲什麼卡上的觀點是不正確的?
UPDATE:
似乎使用this.category.push({id: 1, name: 'a category name'})
是工作,但我會寧願刷新整個數組,所以我可以訂購我想從SQL查詢的方式。
Subject has been posted on ionic forum as well
動態標籤似乎是開放問題https://github.com/driftyco/ionic/issues/10632和https:// github上。 com/driftyco/ionic/issues/10805 –
Bummer ...感謝您提供此信息。解決方案是實施我自己的標籤嗎?我不太喜歡這個解決方案,但我想我沒有其他選擇,我不能等待幾周。 – Brieuc
您是否嘗試過離子頁面生命週期? https://saniyusuf.com/ionic-by-component-page-lifecycle/在構造函數中,您訂閱了類別:創建的事件。您是否需要取消訂閱並在刷新時重新訂閱,也可以將服務呼叫夾在中間? – JGFMK