4

I am currently using ionic 4 and angular 8.

How do I implement by ion slides to be only viewed once by first-time users? I have seen lots of solutions and ways on how to do it on Ionic 1/2 but I see none on ionic 4. Please advise.

2
  • What do you mean "to be only viewed once by first-time users"? That when you are in a page with a ion-slide then you leave this page and then comeback, the slide does not show anymore?
    – Akai shur
    Jan 30, 2020 at 7:49
  • Why don't you try implementing the solutions that are in ionic 1/2 in your project?coz most of them work even in ionic 4. Jan 30, 2020 at 10:16

1 Answer 1

5

Here's one possible solution. It involves using local storage. Just store a key/value pair in storage and look it up when the app starts. If the key exists then do not show the slider. Below is an example of its implementation. This is not fine-tuned but hopefully will get the point across...

Be sure you have Ionic Capacitor enabled. If you do not, run this command:

ionic integrations enable capacitor

Then install Ionic Storage and Sqlite

 npm install @ionic/storage --save
 npm install cordova-sqlite-storage --save

import Ionic Storage in app.module.ts

... all of your other imports
import {IonicStorageModule} from '@ionic/storage';

@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [
            ... all of your other imports, 
            IonicStorageModule.forRoot()
           ],
  providers: [
    StatusBar,
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],
  bootstrap: [AppComponent]
})

Create a storage servive

ionic g service storage

Add a couple of functions to get and save to storage.

storage.service.ts

import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';

@Injectable({
  providedIn: 'root'
})
export class StorageService {

  firstTime: boolean;

  constructor(private storage: Storage) { }

  saveFirstTimeLoad(): void {
    this.storage.set('firstTime', true);
  }

  isFirstTimeLoad(): void {
    this.storage.get("firstTime").then((result) => {
      if (result != null) {
        this.firstTime = false;
      }
      else {
        this.firstTime = true;
      }
    })
  }
}

Initialize the service in app.component.ts

... all of the other imports
import { StorageService } from './storage.service';

export class AppComponent {
    constructor(... all of the other initializations,
                private storageService: StorageService) {
        this.storageService.isFirstTimeLoad();
    }

Then in your page component assign a property to use in the html

export class HomePage implements OnInit {

  firstTime: boolean;
  constructor(private storageService: StorageService) {  }

  ngOnInit() {
    this.firstTime = this.storageService.firstTime;  

    //if first time update first time 
    if(this.firstTime){
      this.storageService.saveFirstTimeLoad();
    }
  }

}

Finally use an ngIf to decide whether to render the component or not.

<ion-item *ngIf="firstTime">
  <ion-label>
     First Time!
  </ion-label>
</ion-item>
<ion-item *ngIf="!firstTime">
  <ion-label>
    NOT First Time
  </ion-label>
</ion-item>

Hope this helps.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.