Alligator.io
The ion-slides component available with Ionic 2 makes it easy to create an intro slider for your app. This can be used as a quick tutorial on how to use the app or a showcase of the features.
Implementing the intro slider is very straightforward, with the only part that can be a bit more tricky being a way to show that intro slider only once, the first time the app is opened. For that, we’ll use Ionic Storage.
You can use the Ionic CLI to add the Intro page:
$ ionic g page Intro
Then import the new page in your app module and add it to the declarations and entryComponents. Also make sure that you’re importing Storage and including it in the list of providers:
// ...
import { Storage } from '@ionic/storage';
import { IntroPage } from '../pages/intro/intro';
In this example Home.ts is our default home page component, where users will be taken after viewing the intro slider or once they’ve seen the intro slider once already.
Go ahead and import the Intro page, NavController and Storage in your home page component. You should also inject NavController and Storage in the constructor:
// ...
import { NavController } from 'ionic-angular';
import { Storage } from '@ionic/storage';
import { IntroPage } from '../pages/intro/intro';
constructor (public navCtrl: NavController, public storage: Storage) {}
And inside the home page component class let’s use Ionic’s ionViewDidLoad hook to check if an intro-done key has been set in storage. If it hasn’t, we set it so that the intro page doesn’t get shown again and then use NavController to show the intro page with our slider. Notice how we use navCtrl.setRoot instead of navCtrl.push, to make sure that our intro page doesn’t get a back button:
ionViewDidLoad() {
this.storage.get('intro-done').then(done => {
if (!done) {
this.storage.set('intro-done', true);
this.navCtrl.setRoot(IntroPage);
}
});
}
The setup work is now done and we can focus on the slider and the intro page itself. We define the slider with the ion-slides component and the individual slides with ion-slide:
<ion-content>
<ion-slides pager="true" parallax="true" padding>
<ion-slide>
<img src="assets/img1.svg">
<h1>Welcome to my app!</h1>
</ion-slide>
<ion-slide>
<img src="assets/img2.svg">
<h1>All of the features</h1>
<p>Here's what you can do with the app...</p>
</ion-slide>
<ion-slide>
<img src="assets/img3.svg">
<h1>Get started now!</h1>
<button ion-button outline small (click)="navHome()">
Start using the app
</button>
</ion-slide>
</ion-slides>
</ion-content>
The ion-slides component can take a bunch of different options. Here we set pager and parallax to true. Pager adds small bullet at the bottom of the slides to show the number of slides and the currently active slide. You can refer to the official documentation for a list of all the options.
Note that the way the options are passed to ion-slides is new since Ionic 2 RC5, and will be the way to go forward.
Notice how we don’t have any navbar on the page, to let the slider take the full height of the page.
The button on the last slide calls a navHome method that will set the Home page back as the root page. Simply define the method in the Intro page component class, making sure that NavController has been imported and injected in the constructor and that the Home page is imported:
// ...
import { NavController } from 'ionic-angular';
import { HomePage } from '../home/home';
// ...
export class IntroPage {
constructor(public navCtrl: NavController) {}
Once final touch, the pager bullets for the slider are blue by default, so here we changed their color with a small addition to the Sass file of the Intro page:
page-intro {
.swiper-pagination-bullet-active {
background: #A713E8;
}
}
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
error TS2304: Cannot find name ‘ionViewDidLoad’. [ng] [ng] 32 ionViewDidLoad() { [ng] ~~~~~~~~~~~~~~
in code:
ionViewDidLoad() { this.storage.get(‘intro-done’).then(done => { if (!done) { this.storage.set(‘intro-done’, true); this.navCtrl.setRoot(IntroPage); } }); }