首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带firebase的Ionic 4-离线工作时间为1/2

带firebase的Ionic 4-离线工作时间为1/2
EN

Stack Overflow用户
提问于 2019-10-18 23:29:44
回答 1查看 43关注 0票数 0

首先,我是一个开发新手,所以请多多关照。

这就是我的问题。

我必须开发一个应用程序在离子4与firebase数据库。

我在ngOnInit函数中的listingComponent上有一个数据库中的目录列表,下面是我的代码:

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';
import { NavigationExtras } from '@angular/router';
import { NavController } from '@ionic/angular';
import { AuthentificationService } from 'src/app/services/authentification.service';
import * as firebase from 'firebase/app';
@Component({
  selector: 'app-listing-directory',
  templateUrl: './listing-directory.component.html',
  styleUrls: ['./listing-directory.component.scss'],
})

export class ListingDirectoryComponent implements OnInit {

constructor(
    private navCtrl: NavController,
    private auth: AuthentificationService) {

  }

ngOnInit() {
    var userUid = this.auth.getUidUser();
    var db = firebase.firestore();
    db.collection('users').doc(userUid).collection('directory').onSnapshot(res => {
      let data = [];
      res.forEach(res => {
        data.push({
          "id": res.id,
          "title": res.data().title,
          "subtitle": res.data().description,
          "image": "assets/imgs/gallery/full-gallery-content-8/12.jpg",
          "items": []
        })
        this.directories = data;
      })
    })

  }

这是我的观点:

代码语言:javascript
复制
<ion-app>

  <ion-header>
    <ion-toolbar box-shadow style="padding-right:10px;">
      <ion-title>
        Liste des dossiers
      </ion-title>
      <ion-button (click)="switchToAddDirectory()" size="small" style="--background: #FB5135; --background-focused: #FB5135; --background-hover: #FB5135;" class="sm-button button-small " slot="end">
        <img style="max-width: 50%;" src="../../../assets/icon/add-plus-button.png">
      </ion-button>
    </ion-toolbar>
  </ion-header>

  <!-- Content -->
  <ion-content >

    <!-- PAGE 3 -->
    <cs-image-gallery-layout-3 [data]="directories" (onItemClick)="onItemClick($event)">
    </cs-image-gallery-layout-3>

  </ion-content>
  <ion-footer style="background:white; padding-right:10px " class="ion-text-end">
      <ion-button (click)="signOut()" size="small" style="--background: #FB5135; --background-focused: #FB5135; --background-hover: #FB5135;" class="sm-button button-small " slot="end">
          Deconnexion
        </ion-button>
  </ion-footer>
</ion-app>

和我的cs-image-gallery-layout-3组件

代码语言:javascript
复制
import { Component, Output, EventEmitter, Input, OnChanges } from '@angular/core';
import { NavController } from '@ionic/angular';

@Component({
  selector: 'cs-image-gallery-layout-3',
  templateUrl: 'image-gallery-layout-3.page.html',
  styleUrls: ['image-gallery-layout-3.page.scss'],
})
export class ImageGalleryLayout3Page implements OnChanges {
  @Input() data: any;

  @Output() onItemClick = new EventEmitter();
  @Output() onFavorite = new EventEmitter();

  constructor(private navCtrl: NavController) {


   }

  ngOnInit(): void {
    //Called after the constructor, initializing input properties, and the first call to ngOnChanges.
    //Add 'implements OnInit' to the class.
    console.log(this.data)
  }
  ngOnChanges(changes: { [propKey: string]: any }) {
    console.log(changes)
    this.data = changes['data'].currentValue;
  }

下面是我的组件视图:

代码语言:javascript
复制
<ion-grid>
  <ion-row *ngIf="data != null">
    <ion-col class="ion-no-margin" size="12" size-md="6" >
      <ion-card class="ion-no-margin" background-size box-shadow [ngStyle]="{'background': 'white'}"
        (click)="redirectToAdd($event)">
        <ion-card-content transparent class="ion-text-center">
          <h2 text-size-xl text-color-accent font-bold class="ion-text-wrap">Ajouter Dossier</h2>
          <h2 text-size-xs text-color-primary font-medium class="ion-text-wrap">Cliquer pour ajouter un dossier</h2>
        </ion-card-content>
      </ion-card>
    </ion-col>
    <ion-col class="ion-no-margin" size="12" size-md="6" *ngFor="let item of data ;let i = index ">
      <ion-card class="ion-no-margin" background-size box-shadow
        [ngStyle]="{'background-image': 'url(' + item.image + ')'}" (click)="onItemClickFunc(item, i, $event)">
        <ion-card-content transparent class="ion-text-center">
          <h2 text-size-xl text-color-accent font-bold class="ion-text-wrap">{{item.title}}</h2>
          <h2 text-size-xs text-color-primary font-medium class="ion-text-wrap">{{item.subtitle}}</h2>
        </ion-card-content>
      </ion-card>
    </ion-col>

  </ion-row>
</ion-grid>

所以问题是,当我在没有互联网的情况下启动我的应用程序时,2次中有1次我得到了我的目录显示……

我对我的回复做了一个console.log,来自:

代码语言:javascript
复制
db.collection('users').doc(userUid).collection('directory').onSnapshot(res => {
          let data = [];
          res.forEach(res => {
            data.push({
              "id": res.id,
              "title": res.data().title,
              "subtitle": res.data().description,
              "image": "assets/imgs/gallery/full-gallery-content-8/12.jpg",
              "items": []
            })
            this.directories = data;
          })
        })

1/2的时间是空的^^看起来很奇怪。

请帮帮我

致以最良好的问候。

EN

回答 1

Stack Overflow用户

发布于 2019-10-31 21:44:42

我在app.module.ts中用firebase.firestore().enablePersistence({experimentalTabSynchronization:true})纠正了它

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58453938

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档