Book Image

Ionic 2 Cookbook - Second Edition

By : Hoc Phan
Book Image

Ionic 2 Cookbook - Second Edition

By: Hoc Phan

Overview of this book

Developing real-time apps is the need of the hour, and apps that deal with humongous amounts of user data and real-time information that needs to be updated frequently are in high demand. Currently, one of the most popular frameworks for this task is Ionic Framework, which is undergoing a major makeover. This book will get you started with Ionic and help you create Angular 2 components that interact with templates. From there, you’ll work with Ionic components and find out how to share data efficiently between them. You’ll discover how to make the best use of the REST API to handle back-end services and then move on to animating the application to make it look pretty. You’ll learn to add in a local push notification in order to test the app. You’ll work with Cordova to support native functionalities on both iOS and Android. From there, you’ll get to grips with using the default themes for each platform as well as customizing your own. Finally, you’ll see how best to deploy your app to different platforms. This book will solve all your Ionic-related issues through dedicated recipes that will help you get the best out of Ionic.
Table of Contents (16 chapters)
Ionic 2 Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Setting up a development environment


Before you create your first app, your environment must have the required components ready. These components ensure a smooth process of development, build and test. The default Ionic project folder is based on Cordova's. Therefore, you need the Ionic CLI to automatically add the correct platform (that is, iOS, Android, or Windows phone) and build the project. This will ensure all Cordova plugins are included properly. The tool has many options to run your app in the browser or simulator with live reload.

Getting ready

You need to install Ionic and its dependencies to get started. Ionic itself is just a collection of CSS styles, AngularJS components, and standard Cordova plugins. It's also a command-line tool to help manage all technologies, such as Cordova and Bower. The installation process will give you a command line to generate the initial code and build the app.

Ionic uses npm as the installer, which is included when installing Node.js. Please install the latest version of Node.js from https://nodejs.org/en/download/.

You will need to install Cordova, ios-sim (iOS Simulator) and Ionic:

$ npm install -g cordova ionic ios-sim

You can install all three components with this single command line instead of issuing three command lines separately. The -g parameter is to install the package globally (not just in the current directory).

For Linux and Mac, you may need to use the sudo command to allow system access, as shown:

$ sudo npm install -g cordova ionic ios-sim

The following are a few common options for an Integrated Development Environment (IDE):

  • Xcode for iOS

  • Android Studio for Android

  • Microsoft Visual Studio Code (VS Code)

  • Sublime Text (http://www.sublimetext.com/) for web development

All of these have a free license. You could code directly in Xcode or Android Studio but those are somewhat heavy-duty for web apps, especially when you have a lot of windows open and just need something simple to code. Sublime Text is free for non-commercial developers but you have to purchase a license if you are a commercial developer. Most frontend developers would prefer to use Sublime Text for coding HTML and JavaScript because it's very lightweight and comes with a well-supported developer community. Sublime Text has been around for a long time and is very user-friendly. However, there are many features in Ionic 2 that make Visual Studio Code very compelling. For example, it has the look and feel of a full IDE without being bulky. You could debug JavaScript directly inside VS Code as well as getting autocomplete (for example, IntelliSense). The following instructions cover both Sublime Text and VS Code, although the rest of this book will use VS Code.

How to do it…

VS Code works on Mac, Windows, and Linux. Here are the instructions

  1. Visit https://code.visualstudio.com.

  2. Download and install for your specific OS.

  3. Unzip the downloaded file.

  4. Drag the .app file into the Applications folder and drag it to Mac's Dock.

  5. Open Microsoft Visual Studio code.

  6. Press Ctrl + Shift + p to open command palette.

  7. Type shell command in command palette.

  8. Click on the Shell Command: Install 'code' command in PATH command to install the script to add Visual Studio Code in your terminal $PATH.

  9. Restart Visual Studio Code to take effect.

  10. Later on, you can just do code. (including the dot) directly from the Ionic project folder and VS Code will automatically open that folder as a project:

    Note

    Note that the following screenshots are done via Mac.

  11. If you decide to use Sublime Text, you will need Package Control (https://packagecontrol.io/installation), which is similar to a Plugin Manager. Since Ionic uses Sass, it's optional to install the Sass Syntax Highlighting package.

  12. Navigate to Sublime Text | Preferences | Package Control:

  13. Go to Package Control: Install Package. You could also just type the commands partially (that is, inst) and it will automatically select the right option:

  14. Type Sass and the search results will show one option for TextMate & Sublime Text. Select that item to install:

There's More…

There are tons of Sublime Text packages that you may want to use, such as HTML, JSHint, JSLint, Tag, and ColorPicker You can visit https://sublime.wbond.net/browse/popular for additional needs.