16 min read

In this article by Gaurav Saini the authors of the book Hybrid Mobile Development with Ionic, we will learn following topics:

  • Building vPlanet Commerce
  • Ionic 2 components

(For more resources related to this topic, see here.)

Building vPlanet Commerce

The vPlanet Commerce app is an e-commerce app which will demonstrate various Ionic components integrated inside the application and also some third party components build by the community.

Let’s start by creating the application from scratch using sidemenu template:

Hybrid Mobile Development with Ionic

You now have the basic application ready based on sidemenu template, next immediate step I took if to take reference from ionic-conference-app for building initial components of the application such aswalkthrough.

Let’s create a walkthrough component via CLI generate command:

$ ionic g page walkthrough

As, we get started with the walkthrough component we need to add logic to show walkthrough component only the first time when user installs the application:

// src/app/app.component.ts
// Check if the user has already seen the walkthrough
this.storage.get('hasSeenWalkThrough').then((hasSeenWalkThrough) => {
if (hasSeenWalkThrough) {
this.rootPage = HomePage;
} else {
this.rootPage = WalkThroughPage;

So, we store a boolean value while checking if user has seen walkthrough first time or not. Another important thing we did create Events for login and logout, so that when user logs into the application and we can update Menu items accordingly or any other data manipulation to be done:

// src/app/app.component.ts

export interface PageInterface {
title: string;
component: any;
icon: string;
logsOut?: boolean;
index?: number;
tabComponent?: any;
export class vPlanetApp {
   loggedInPages: PageInterface[] = [
    { title: 'account', component: AccountPage, icon: 'person' },
    { title: 'logout', component: HomePage, icon: 'log-out', logsOut: true }
  loggedOutPages: PageInterface[] = [
    { title: 'login', component: LoginPage, icon: 'log-in' },
    { title: 'signup', component: SignupPage, icon: 'person-add' }

listenToLoginEvents() {
this.events.subscribe('user:login', () => {

this.events.subscribe('user:logout', () => {

enableMenu(loggedIn: boolean) {
this.menu.enable(loggedIn, 'loggedInMenu');
this.menu.enable(!loggedIn, 'loggedOutMenu');

// For changing color of Active Menu

isActive(page: PageInterface) {
if (this.nav.getActive() && this.nav.getActive().component === page.component) {
return 'primary';


Next we have inside our app.html we have multiple items depending upon whether user is loggedin or logout:

// src/app/app.html

{{'menu' | translate}}

        {{'navigate' | translate}}

        {{'account' | translate}}


        {{'navigate' | translate}}

        {{'account' | translate}}

As, our app start mainly from app.html so we declare rootPage here:

Let’s now look into what all pages, services, and filter we will be having inside our app. Rather than mentioning it as a bullet list, the best way to know this is going through app.module.ts file which has all the declarations, imports, entryComponents and providers.

// src/app/app.modules.ts

import { NgModule, ErrorHandler } from '@angular/core';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { TranslateModule, TranslateLoader, TranslateStaticLoader } from 'ng2-translate/ng2-translate';
import { Http } from '@angular/http';
import { CloudSettings, CloudModule } from '@ionic/cloud-angular';
import { Storage } from '@ionic/storage';
import { vPlanetApp } from './app.component';

import { AboutPage } from '../pages/about/about';
import { PopoverPage } from '../pages/popover/popover';
import { AccountPage } from '../pages/account/account';
import { LoginPage } from '../pages/login/login';
import { SignupPage } from '../pages/signup/signup';
import { WalkThroughPage } from '../pages/walkthrough/walkthrough';
import { HomePage } from '../pages/home/home';
import { CategoriesPage } from '../pages/categories/categories';
import { ProductsPage } from '../pages/products/products';
import { ProductDetailPage } from '../pages/product-detail/product-detail';
import { WishlistPage } from '../pages/wishlist/wishlist';
import { ShowcartPage } from '../pages/showcart/showcart';
import { CheckoutPage } from '../pages/checkout/checkout';
import { ProductsFilterPage } from '../pages/products-filter/products-filter';
import { SupportPage } from '../pages/support/support';
import { SettingsPage } from '../pages/settings/settings';
import { SearchPage } from '../pages/search/search';
import { UserService } from '../providers/user-service';
import { DataService } from '../providers/data-service';
import { OrdinalPipe } from '../filters/ordinal';

// 3rd party modules
import { Ionic2RatingModule } from 'ionic2-rating';

export function createTranslateLoader(http: Http) {
return new TranslateStaticLoader(http, './assets/i18n', '.json');

// Configure database priority
export function provideStorage() {
return new Storage(['sqlite', 'indexeddb', 'localstorage'], { name: 'vplanet' })

const cloudSettings: CloudSettings = {
'core': {
'app_id': 'f8fec798'

declarations: [
imports: [
provide: TranslateLoader,
useFactory: createTranslateLoader,
deps: [Http]
bootstrap: [IonicApp],
entryComponents: [
providers: [
{provide: ErrorHandler, useClass: IonicErrorHandler},
{ provide: Storage, useFactory: provideStorage }, 

export class AppModule {}

Ionic components

There are many Ionic JavaScript components which we can effectively use while building our application. What’s best is to look around for features we will be needing in our application. Let’s get started with Home page of our e-commerce application which will be having a image slider having banners on it.


Slides component is multi-section container which can be used in multiple scenarios same astutorial view or banner slider. component have multiple elements which can be dragged or swipped left/right. Slides have multiple configuration options available which can be passed in the ion-slides such as autoplay, pager, direction: vertical/horizontal, initialSlide and speed.

Using slides is really simple as we just have to include it inside our home.html, no dependency is required for this to be included in the home.ts file:

// Defining banners image path

export class HomePage {

products: any;
banners: String[];

constructor() {
this.banners = [


Lists are one of the most used components in many applications. Inside lists we can display rows of information. We will be using lists multiple times inside our application such ason categories page where we are showing multiple sub-categories:

// src/pages/categories/categories.html

Fetching Categories ....





Loading and toast

Loading component can be used to indicate some activity while blocking any user interactions. One of the most common cases of using loading component is HTTP/ calls to the server, as we know  it takes time to fetch data from server, till then for good user experience we can show some content showing Loading .. or Login wait .. for login pages.

Toast is a small pop-up which provides feedback, usually used when some action  is performed by the user. Ionic 2 now provides toast component as part of its library, previously we have to use native Cordova plugin for toasts which in either case can now be used also.

Loading and toast component both have a method create. We have to provide options  while creating these components:

// src/pages/login/login.ts

import { Component } from '@angular/core';
import { NgForm } from '@angular/forms';
import { NavController, LoadingController, ToastController, Events } from 'ionic-angular';
import { SignupPage } from '../signup/signup';
import { HomePage } from '../home/home';

import { Auth, IDetailedError } from '@ionic/cloud-angular';

import { UserService } from '../../providers/user-service';

selector: 'page-user',
templateUrl: 'login.html'
export class LoginPage {
login: {email?: string, password?: string} = {};

submitted = false;

constructor(public navCtrl: NavController, 
public loadingCtrl: LoadingController,
public auth: Auth,
public userService: UserService,
public toastCtrl: ToastController,
public events: Events) { }

onLogin(form: NgForm) {
this.submitted = true;

if (form.valid) {
// start Loader
let loading = this.loadingCtrl.create({
content: "Login wait...",
duration: 20

this.auth.login('basic', this.login).then((result) => {
// user is now registered
}, (err: IDetailedError) => {

showToast(response_message:any) {
let toast = this.toastCtrl.create({
message: (response_message ? response_message : "Log In Successfully"),
duration: 1500

onSignup() {

As, you can see from the previouscode creating a loader and toast is almost similar at code level. The options provided while creating are also similar, we have used loader here while login and toast after that to show the desired message.

Setting duration option is good to use, as in case loader is dismissed or not handled properly in code then we will block the user for any further interactions on app. In HTTP calls to server we might get connection issues or failure cases, in that scenario it may end up blocking users.

Tabs versussegments

Tabs are easiest way to switch between views and organise content at higher level. On the other hand segment is a group of button and can be treated as a local  switch tabs inside a particular component mainly used as a filter. With tabs we can build quick access bar in the footer where we can place Menu options such as Home, Favorites, and Cart. This way we can have one click access to these pages or components. On the other hand we can use segments inside the Account component and divide the data displayed in three segments profile, orders and wallet:

// src/pages/account/account.html






// Order List data to be shown here
// Wallet statement and transaction here.

Hybrid Mobile Development with Ionic

This is how we define a segment in Ionic, we don’t need to define anything inside the typescript file for this component. On the other hand with tabs we have to assign a component for  each tab and also can access its methods via Tab instance. Just to mention,  we haven’t used tabs inside our e-commerce application as we are using side menu. One good example will be to look in ionic-conference-app (https://github.com/driftyco/ionic-conference-app) you will find sidemenu and tabs both in single application:

// We currently don’t have Tabs component inside our e-commerce application
// Below is sample code about how we can integrate it.

import { HomePage } from '../pages/home/home';
import { WishlistPage } from '../pages/wishlist/wishlist';
import { ShowcartPage } from '../pages/showcart/showcart';

export class TabsPage {

@ViewChild('showTabs') tabRef: Tabs;

// this tells the tabs component which Pages
// should be each tab's root Page

Home = HomePage;
Wishlist = WishlistPage;
Cart = ShowcartPage;

constructor() {


// We can access multiple methods via Tabs instance
// select(TabOrIndex), previousTab(trimHistory), getByIndex(index)
// Here we will console the currently selected Tab.

ionViewDidEnter() {

Properties can be checked in the documentation (https://ionicframework.com/docs/v2/api/components/tabs/Tabs/) as, there are many properties available for tabs, like mode, color, tabsPlacement and tabsLayout. Similarly we can configure some tabs properties at Config level also, you will find here what all properties you can configure globally or for specific platform. (https://ionicframework.com/docs/v2/api/config/Config/).


Alerts are the components provided in Ionic for showing trigger alert, confirm, prompts or some specific actions. AlertController can be imported from ionic- angular which allow us to programmatically create and show alerts inside the application. One thing to note here is these are JavaScript pop-up not the native platform pop-up. There is a Cordova plugin cordova-plugin-dialogs (https://ionicframework.com/docs/v2/native/dialogs/) which you can use if native dialog UI elements are required.

Currently five types of alerts we can show in Ionic app basic alert, prompt alert, confirmation alert, radio and checkbox alerts:

// A radio alert inside src/pages/products/products.html for sorting products

// onClick we call sortBy method 
// src/pages/products/products.ts
import { NavController, PopoverController, ModalController, AlertController } from 'ionic-angular';	

export class ProductsPage {
		public alertCtrl: AlertController
	) {

sortBy() {
let alert = this.alertCtrl.create();
alert.setTitle('Sort Options');

type: 'radio',
label: 'Relevance',
value: 'relevance',
checked: true
type: 'radio',
label: 'Popularity',
value: 'popular'
type: 'radio',
label: 'Low to High',
value: 'lth'
type: 'radio',
label: 'High to Low',
value: 'htl'
type: 'radio',
label: 'Newest First',
value: 'newest'

text: 'OK',
handler: data => {
		// Here we can call server APIs with sorted data
		// using the data which user applied.

alert.present().then(() => {
// Here we place any function that
	   // need to be called as the alert in opened.


Hybrid Mobile Development with Ionic

Cancel and OK buttons. We have used this here for sorting the products according to relevance price or other sorting values.

We can prepare custom alerts also, where we can mention multiple options. Same as in previous example we have five radio options, similarly we can even add a text input box for taking some inputs and submit it. Other than this, while creating alerts remember that there are alert, input and button options properties for all the alerts present in the AlertController component.(https://ionicframework.com/docs/v2/api/components/alert/AlertController/).

Some alert options:

  • title:// string: Title of the alert.
  • subTitle:// string(optional): Sub-title of the popup.
  • Message:// string: Message for the alert
  • cssClass:// string: Custom CSS class name
  • inputs:// array: Set of inputs for alert.
  • Buttons:// array(optional): Array of buttons

Cards and badges

Cards are one of the important component used more often in mobile and web applications. The reason behind cards are so popular because its a great way to organize information and get the users access to quantity of information on smaller screens also. Cards are really flexible and responsive due to all these reasons they are adopted very quickly by developers and companies. We will also be using cards inside our application on home page itself for showing popular products. Let’s see what all different types of cards Ionic provides in its library:

  • Basic cards
  • Cards with header and Footer
  • Cards lists
  • Cards images
  • Background cards
  • Social and map cards

Social and map cards are advanced cards, which is build with custom CSS. We can develop similar advance card also.

// src/pages/home/home.html



{{prdt.price | currency }}   {{prdt.listPrice | currency}}

We have used here image card with a image on top and below we have favorite and view button icons. Similarly, we can use different types of cards where ever its required. Also, at the same time we can customize our cards and mix two types of card using their specific CSS classes or elements.

Badges are small component used to show small information, for example showing number of items in cart above the cart icon. We have used it in our e-commerce application for showing the ratings of product.



In this article we have learned, building vPlanet Commerce and Ionic components.

Resources for Article:

Further resources on this subject:


Please enter your comment!
Please enter your name here