【问题标题】:Ionic 3 Tabs are not refreshed after initializedIonic 3选项卡在初始化后不刷新
【发布时间】:2018-02-04 11:29:39
【问题描述】:

每个选项卡使用不同的 navParams 调用相同的组件“EventDetailItemsComponent”。并且从 this.itemService 中检索异步数据(Observable)并按预期显示。

当我第二次切换回同一个标签时,问题就开始了。由于不再调用 Component,因此它显示相同的异步数据。所以标签视图没有更新。

我想要的是每次单击选项卡时调用 EventDetailItemsComponent 。不仅仅是第一次点击。

使用 (ionSelect)="myMethod()" 将异步数据从父组件 EventDetailComponent 传递到子组件 EventDetailItemsComponent 是否正确?

    <ion-tabs no-margin no-padding selectedIndex="1">
        <ion-tab [root]="EventDetailItems1" [rootParams]="{itemTypeId:1, eventId: (eventDetail | async)?.id}" tabTitle="{{'ALL' | translate}}"></ion-tab>
        <ion-tab [root]="EventDetailItems2" [rootParams]="{itemTypeId:2, eventId: (eventDetail | async)?.id}" tabTitle="{{'BOUGHT' | translate}}"></ion-tab>
        <ion-tab [root]="EventDetailItems3" [rootParams]="{itemTypeId:3, eventId: (eventDetail | async)?.id}" tabTitle="{{'LEFT' | translate}}"></ion-tab>
    </ion-tabs>


    export class EventDetailComponent implements OnInit {

        EventDetailItems1: any = EventDetailItemsComponent;
        EventDetailItems2: any = EventDetailItemsComponent;
        EventDetailItems3: any = EventDetailItemsComponent;

        constructor(){ }
    }

子组件EventDetailItemsComponent及其html

    @Component({
        selector: 'event-detail-items',
        styleUrls: ['/event-detail-items.scss'],
        templateUrl: 'event-detail-items.html'
    })
    export class EventDetailItemsComponent implements OnInit, OnChanges  {
        private _itemDetail = new BehaviorSubject<ItemViewModel[]>(null);
        itemDetail = this._itemDetail.asObservable();

        itemToggle: number = 0;
        lastImage: string = null;
        loading: Loading;

        constructor(
            public itemService: ItemService,
            public eventService: EventService,
            public navParams: NavParams,
            public navCtrl: NavController,
            public loadingCtrl: LoadingController,
            private app: App) {

        }

        ngOnInit() {
            let itemParams: GiftItemTabNavParams = JSON.parse(JSON.stringify(this.navParams.data));                
            this.itemService.getItemList(itemParams.eventId, itemParams.itemTypeId).subscribe(x => {
                this._itemDetail.next(x);
            });
        }
    }


          <ion-grid id="gift-list-grid">                
            <ion-row class="gift-list-row" *ngFor="let giftItem of (itemDetail | async)" text-center bottom>
                <ion-col (click)="toggleItemDescription(giftItem.id)" class="gift-item-col-item-pic" col-3>
                    <img src="{{giftItem.giftImagePath}}" />
                </ion-col>
                <ion-col (click)="toggleItemDescription(giftItem.id)" class="gift-item-col-detail" col-6>
                    <ion-label text-wrap class="gift-item-text" no-margin text-left>
                        <span>
                            {{giftItem.giftItemName}}
                        </span>
                        <span>
                            {{giftItem.brand}}
                        </span>
                    </ion-label>
                </ion-col>
                <ion-col (click)="toggleItemDescription(giftItem.id)" class="gift-item-col-gift-count" col-3>
                    <img src="./assets/img/inner-pages/gift_box.png" />
                    <p>
                        {{giftItem.amount}}
                    </p>
                </ion-col>
                <ion-col (click)="toggleItemDescription(giftItem.id)" *ngIf="itemToggle == giftItem.id" col-9>
                    <ion-label text-wrap class="gift-item-description" no-margin text-left>
                        <span>
                            {{giftItem.amount}} {{'AMOUNT' | translate}}
                        </span>
                        <span>
                            {{giftItem.description}}
                        </span>
                    </ion-label>
                </ion-col>
                <ion-col (click)="toggleBuyStatus(giftItem.id, giftItem.isBought, giftItem.giftStatus)" *ngIf="itemToggle == giftItem.id" class="gift-item-col-detail"  col-3>
                    <!--RESERVABLE ITEM-->
                    <img *ngIf="giftItem.giftStatus == 0" src="./assets/img/inner-pages/free.png" />
                    <!--CAN BE UNRESERVED-->
                    <img *ngIf="giftItem.giftStatus == 1" src="./assets/img/inner-pages/unreservable.png" />
                    <!--CAN NOT BE UNRESERVED-->
                    <img *ngIf="giftItem.giftStatus == 2" src="./assets/img/inner-pages/not-unreservable.png" />
                </ion-col>
            </ion-row>
        </ion-grid>

更新:正如 Sampath 所说,Eve​​nts 解决了这个问题。您可以在下面找到用法:

EventDetailComponent - 事件发布:

public eventTabsChanged(itemTypeId) {
            let event = this._eventDetail.getValue();
            this.events.publish('tab:clicked', itemTypeId, event.id);
        }

标签内容EventDetailItemsComponent - 事件订阅

    constructor(
            public itemService: ItemService,       
            public events: Events) {
            events.subscribe('tab:clicked', (itemTypeId, eventId) => {
                this.itemService.getItemList(eventId, itemTypeId).subscribe(x => {
                    this._itemDetail.next(x);
                });
            });
        }

【问题讨论】:

  • 你也可以显示子组件的code 吗? htmlts.
  • @Sampath 我添加了子组件及其 html
  • ion-tab 或 ion-grid 点击你想更新?
  • @rashidnk 您是否建议使用单击并在函数内部使用@Input 将异步数据传递给子组件?
  • 可以展示一下亲子互动的code吗? html 代码?

标签: angular typescript ionic2 ionic3


【解决方案1】:

看来你需要知道如何在parentchild 之间传递数据。所以你可以使用input 属性来做到这一点。

选项 1:

在你的情况下:

<event-detail-items [itemDetail]="itemDetail"></event-detail-items>

请查看offical doc here

child.ts

import { Component, Input } from '@angular/core';

import { Hero } from './hero';

@Component({
  selector: 'hero-child',
  template: `
    <h3>{{hero.name}} says:</h3>
    <p>I, {{hero.name}}, am at your service, {{masterName}}.</p>
  `
})
export class HeroChildComponent {
  @Input() hero: Hero;
  @Input('master') masterName: string;
}

parent.ts

import { Component } from '@angular/core';

import { HEROES } from './hero';

@Component({
  selector: 'hero-parent',
  template: `
    <h2>{{master}} controls {{heroes.length}} heroes</h2>
    <hero-child *ngFor="let hero of heroes"
      [hero]="hero"
      [master]="master">
    </hero-child>
  `
})
export class HeroParentComponent {
  heroes = HEROES;
  master = 'Master';
}

选项2:使用Events

【讨论】:

  • 当我尝试 它抛出这样的错误:“不能绑定到 'itemDetail' 因为它不是 'ion- 的已知属性tab'" 所以我猜 ion-tab 不接受输入参数。
  • 我现在将尝试使用 [rootParams] 传递异步数据。但是 [rootParams] 是我知识的终结,如果它不起作用,我将完全被卡住:)
  • 你不能那样做。因为ion-tab不是你的child组件吗?您需要将其绑定到您的子组件,如下所示:&lt;event-detail-items [itemDetail]="itemDetail"&gt;&lt;/event-detail-items&gt;
  • 我正在考虑将 ion-tab 作为我的子组件。是否有任何等效的方法可以在根组件和 ion-tab 组件之间传递数据?
  • 不,你错了。你不能这样想。你有另一种方法在组件之间传递数据。那就是Events。看这个:ionicframework.com/docs/api/util/Events
猜你喜欢
  • 2017-09-12
  • 2018-06-02
  • 1970-01-01
  • 2012-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-30
相关资源
最近更新 更多