简体   繁体   English

当我打开页面时,对Firebase的调用仅发生一次,而当我导航回它时则永远不会发生

[英]Call to Firebase happens only once when i open the page , then it never happens when i navigate back to it

Am doing a call to the firebase from my ionic 4 project that gets a list of items. 我正在从ionic 4项目中调用firebase,该项目获取项目列表。 it only happens once I launch the app for the first time, then if I navigate to another page then back to this page. 只有当我第一次启动该应用程序时,它才会发生,然后,如果我导航到另一个页面,则返回此页面。 it shows a blank list. 它显示一个空白列表。 here is my code : 这是我的代码:

import { Component, OnInit } from '@angular/core';
import { DressService, Dress } from '../services/dress.service';

@Component({
  selector: 'app-list',
  templateUrl: 'list.page.html',
  styleUrls: ['list.page.scss']
})
export class ListPage implements OnInit {
  dress: Dress[];
  constructor(private dressService: DressService,
  ) {
    this.dressService.getDresses().subscribe(res => {
      this.dress = res;
    });
  }

  ngOnInit() {

  }
  // add back when alpha.4 is out
  // navigate(item) {
  //   this.router.navigate(['/list', JSON.stringify(item)]);
  // }
}

And here is how I view the list in my HTML page 这就是我在HTML页面中查看列表的方式

    <ion-list>
        <ion-item *ngFor="let item of dress" >
            <img src="{{item.image_1}}">
            <ion-label dir="rtl">
                <small>{{item.category}}</small>
                <br>
                <ion-text color="secondary">
                    <b>{{item.title}}</b>
                </ion-text>
                <br>
                <ion-badge color="danger">{{item.price}} EGP</ion-badge>
                <br>
                <small>{{item.city}}</small>
            </ion-label>
        </ion-item>
    </ion-list>

are you using a router in your app? 您在应用中使用路由器吗? If so, you could subscribe to a Router instance and do something when the route changes. 如果是这样,您可以订阅路由器实例,并在路由更改时执行一些操作。

       constructor(private router: Router) {
        router.events.subscribe(() => shouldGetDresses());
       }

       private shouldGetDresses() {
        this.dressService.getDresses().subscribe(res => {
        this.dress = res;
       });
      }

Based on @SeanKPS Comment, I needed to use unsubscribe on the ngOnDestroy . 基于@SeanKPS评论,我需要使用unsubscribengOnDestroy

Changed my code to be like this : 改变我的代码是这样的:

import { Component, OnInit } from '@angular/core';
import { DressService, Dress } from '../services/dress.service';
import { Router } from '@angular/router';
import { Subscription, ObjectUnsubscribedError } from 'rxjs';

@Component({
  selector: 'app-list',
  templateUrl: 'list.page.html',
  styleUrls: ['list.page.scss']
})
export class ListPage implements OnInit {
  dress: Dress[];
  mySub: any;
  constructor(private dressService: DressService,
              private router: Router,
  ) { }
  ngOnInit() {
    this.mySub = this.dressService.getDresses().subscribe(res => {
      this.dress = res;
    });
  }
  ngOnDestroy() {
    // prevent memory leak when component destroyed
    this.mySub.unsubscribe();
  }
  // add back when alpha.4 is out
  // navigate(item) {
  //   this.router.navigate(['/list', JSON.stringify(item)]);
  // }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular和Node JS SPA-当我在组件之间导航时,幕后会发生什么? - Angular and node js SPA- what happens behind the scenes when i navigate between components? 触摸事件仅在 canvas 上绘制时发生一次 - Touch event only happens once when drawing on canvas 当我导航到 Angular 6 中的后页时如何清除下拉列表值 - How to clear the dropdowns values when i navigate to back page in Angular 6 在角度2上,当我从一个大型库中导入一个模块时,构建会发生什么? - On angular 2 what happens on build when I import only one module from a big library? 当我在Ionic 2中导入未使用的添加物时会发生什么? - What will happens when I import unused additions in Ionic 2? 当我在 Angular 中单击后退按钮时,我无法导航到索引页面 - I cant navigate to the index page, when i click back button in Angular 导航到另一页时为空列表 - Empty list when I navigate to another page Firebase:每隔5分钟没有写操作时更新数据库 - Firebase: update database when no write operation happens in 5 minutes interval 如果发生警报,​​则每小时仅显示一次警报 - display alert only once in an hour if it happens jhipster 过滤器结果正确返回,但是当我单击 UI 导航到结果集中的第 2 页时,我再次获得未过滤的结果 - jhipster filter results are returned correclty but when I click the UI to navigate to page 2 in the result set I get the unfiltered results back again
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM