TL;DR: In this article, you will learn about what’s new in Ionic 4, changes in the existing features, how to migrate your Ionic 3 app to the Ionic 4 app, and about using Ionic with Stencil and Capacitor. Finally, you will create a simple application using Ionic 4 to see the new version of this framework in action. If you need, you can check the final code of the app that you will create on this GitHub repository.
Ionic 4 Beta: Learn What’s New
The Ionic framework is quite a famous name in the mobile application development area. Ever since this framework came into existence, it’s been tightly coupled to Angular. For example, Ionic 1 was built on Angularjs, and Ionic 2 and Ionic 3 are based on Angular2+.
Starting with Ionic 4, things are now different. Ionic components are now built using Web Components with the help of Stencil. What is interesting about Web Components is that you can use them with any framework or even without a framework. That makes it possible to use Ionic with Angular, React, Vue, or whatever other frameworks you can imagine.
Right now, the new (but still beta) version 4 of Ionic is available via the @ionic/core
NPM package. Also, there are Angular bindings available via the @ionic/angular
NPM package. Alongside that, the official bindings for React and Vue are currently in development.
This article will mostly focus on @ionic/angular
. Here, you will learn how to create new Ionic/Angular applications, make navigation, migrate the Ionic3 app to the Ionic4 app, and you will also create a demo application using Ionic/Angular.
Ionic 4 and Angular
In this section, you will read about the changes that Ionic 4 brought with respect to Angular. In other words, you will be learning about the changes in @ionic/angular@v4
.
Routing in Ionic 4 and Angular
One of the biggest changes in Ionic 4 is the support of Angular Router. With Ionic 4, you can use the @angular/router
to handle the navigation of your Ionic apps. However, keep in mind that Ionic 4 still supports the Push/Pop ion-nav
navigation system.
In a typical Angular application, you have to use the router-outlet
component to project the view. In Ionic 4 applications, you will use a version of router-outlet
called ion-router-outlet
. You have to add the following HTML element in the template where you want to project the view.
After adding the ion-router-outlet
component, you will need to create a router config, which is required to configure the navigation paths of your Angular application. This can be done as follows:
Angular Router parses the router config from top to bottom. The above config basically conveys the follows:
- If users navigate to the
/
route, then your app will redirect them the/home
route. - If users navigate to
/home
route, then your app will load theHomePageModule
. - If users navigate to
/user/:id
route, then your app will load theUserPageModule
. In this case,:id
is a navigation parameter. For example: if users try to reach/users/u101
,u101
is the value:id
will get. - If users open any route which doesn’t match any path in your config, your app will redirect them to
/home
.
Note: the
loadChildren
property in a route is used for Lazily Loading an Angular Module. By default, if you generate a page using Ionic CLI, it will be lazily loaded.
Then you have to provide these routes to the RouterModule
in the root @NgModule
of your Angular application, as follows:
Now, when your Angular application bootstraps, it will load the default route specified in your Router configuration. But the rest of the navigation will depend upon users’ interaction with the app per se. To trigger navigation, you two options: one is declarative, and the other one is imperative.
For example, to define a declarative navigaiton in your Angular application, you can use the routerLink
directive:
With that, if your users click on the above anchor element, the app will trigger navigation to the /home
route. If you come from previous versions of the Ionic framework, you will probably remember that you used to have nice animation flows that appeared when you were pushing a new view or page to your users. This is still possible by using routerDirection
directive along with routerLink
.
Basically, there are three possible values for this directive:
forward
: It’s just like pushing a view to the navigation stack.backward
: It’s like popping a view from the navigation stack.root
: It behaves likesetRoot
method of NavController. This means that it will make the page that you are about to navigate to the root page of the application.
For example, to use the forward
option you could have something like this:
Now, when it comes to define imperative navigation in your app, you can call the navigateForward
, navigateBack
, and navigateRoot
methods of NavController
:
Using Ionic and Angular CLI
With Ionic 4, Ionic CLI projects are almost similar to Angular CLI projects. As a matter of fact, Ionic CLI uses Angular CLI internally.
To use the latest Ionic CLI version, make sure you install it:
Next step, if you were going to create a new app, you would issue commands with starter templates as below (don’t really worry about running this now, you will soon create your app):
Note:
--type=angular
tells Ionic CLI to generate an Angular-based project.
When you scaffold your app, you will get a file structure that looks like the following:
e2e
: This is a directory that contains default end-to-end test code and where you can write your own tests.resources
: This is a directory that contains resources like icons and images for your mobile app.src
: This is where you will put the source code of your Ionic application.angular.json
: A file that contains all the configuration of your project. You can configure yourproduction
ordev
environment and lots of the other things. This file makes it possible to use Angular CLI with Ionic projects.config.xml
: The config file for Cordova.ionic.config.json
: The config file used by Ionic CLI.tsconfig.json
: The config file that defines rules for the TypeScript compiler.tslint.json
: The config file that defines rules for the TypeScript linter.
Just like with Angular CLI, after scaffolding your app, you will be able to run a hello-world app by issuing a single command:
Theming in Ionic 4
With Ionic 4, the Ionic team is adopting web standards. In the new version of the framework, all of the Ionic Components are built using Stencil (which means that they are relying on the Web Component spec). With respect to theming, Ionic is using CSS variables.
By default, Ionic comes with nine colors, and each one of these has two extra variants called as shade and tint. You can easily edit the default color values along with their shade and tint variant.
Color Structure
Before you start modifying existing colors in Ionic, you need to understand some concepts. Each default color in Ionic has multiple properties, kind of like having multiple layers of colors. Each of these properties is applied to a different part of a particular component. These properties are as follows:
base
: The main color that will be applied to the component.shade
: A bit darker than the base color and used to add shades.tint
: It’s a bit lighter than the base color.contrast
: Contrast color is a color which is easily visible (perceptible) on the base color. For example, if the base is black, then contrast would be white. Basically, contrast is used for showing text on components like button, list, etc.
Following is an excerpt of the default primary color values from an Ionic project:
Modifying Existing Color
When you open the /src/theme/variable.scss
file, you can see all nine default colors in that file. You can modify any of the values as shown below:
Above example changes the primary color (along with it’s shade
and tint
properties) to #00ff00
for the whole application. You can also change this particular color for a particular component as follows:
Migrating from Ionic 3 to Ionic 4
Migration is always a difficult and painful subject. However, the team behind the Ionic framework worked hard to make things as easy as possible. The following list presents some tasks that you will have to do to migrate from Ionic 3 to Ionic 4:
- Angular Router: As mentioned earlier, you need to migrate your Push/Pop navigation to Angular Router navigation. You can follow the step mentioned above to do so.
- NavParams Service: With Angular Router, you can’t use the beloved
NavParams
service, but there are many alternatives to it. One of the best would be using NgRx to manage the state. It’s fairly easy to use NgRx to mimic the same behavior of theNavParams
service. However, there is a file inside Ionic 4’s GitHub repo which might be hinting atNavParams
debut in Ionic 4. - LifeCycleEvents: Since you will not use the
ion-nav
navigation element, you can’t useionViewWillLoad
or similar life cycle events. However, you can use Angular’s life cycle hooks in their place. To learn more about Angular lifecycle hooks, take a look here. - Markup: There are lots of changes in markup due to the fact that Ionic uses the Web component spec. It’s out of scope of this blog post to talk about all those changes. Nevertheless, it is important to notice that the Ionic team introduced a migration linter for markup that you can use.
Using the Migration Linter
It’s definitely fun to learn things and sometimes you need to change things manually. But if at a certain point you can do things automatically, you should opt for it. As mentioned, the Ionic team also introduced a migration linter tool that you can use to migrate your Ionic 3 markup to Ionic 4 markup.
To use it, you will have to install the lint rules:
Then you will need to create a file called ionic-migration.json
at the root of your project and paste in the following JSON:
Then you can lint your project by running the following command:
Note :
npx
is a new utility that comes with NPMv5.2.0
. This tool allows you to run local Node executables from thenode_modules
directory.
Ionic 4 and Stencil
Before you start digging into this section, you should learn a few things about Stencil. Basically speaking, Stencil is a compiler that lets you create UI Components and Web Apps using the Web Component specification. Coding with Stencil is kind of like a mixture of Angular and React. While developing with Stencil, you will use Decorators, JSX, and TypeScript to name a few.
A typical Stencil Component looks like follows:
Then, to use your new web component, you can just add it to your HTML like any other element:
Using Ionic with Stencil
You might be wondering, why would I want to read about Stencil in an Ionic article? In other words, how would you benefit from learning about Ionic and Stencil?
The best answer to that question is that Ionic Core is built using the Stencil compiler. That is, they play really nicely with each other. Moreover, Stencil has enough plug-ins that you can use to write a complete application. Also, Stencil comes with an Ionic PWA (Progressive Web App) starter that can be used to create Ionic applications.
As you will see here, it’s fairly easy to get started with Ionic and Stencil. To do so, you will need to run the following command to trigger Stencil CLI:
This command will ask you questions like:
- Pick a starter: First, it will ask you to pick a starter. There is three starters for now:
ionic-pwa
, which is Ionic with the PWA toolkit;app
, a minimal starter to build stencil app or website; andcomponent
, a collection of web components to test the PWA capabilities, select theionic-pwa
option. - Project name: For this, you can add any name that you can come up with.
- Confirm?: Finally, it will ask you to confirm. Press y to confirm.
Now you can run the application as follows:
To build the application, you can run the following command:
The above command will generate a www
directory with all the files required to deploy or build a mobile application using Capacitor or Cordova.
Using Stencil Plugins with Ionic 4
With Ionic, there are some plug-ins available to do certain things, for example:
@stencil/router
: This is a router plugin for stencil applications that can be used to manage routing in your apps.@stencil/redux
: If you like managing the state of your apps with Redux style, you can do that via this package.
Furthermore, you can use any JavaScript library with Stencil applications, because Stencil components transpile into plain JavaScript web components.
Building Mobile Apps with Capacitor on Ionic 4
Capacitor is the latest kid on the block when it comes to hybrid app development. What is impressive about Capacitor is that you can literally drop it inside any JavaScript project.
Capacitor is a new tool doing the same thing (like Cordova for example) with a different API for creating plugins. But in the end, both things are running your application inside a web view (i.e., an embedded browser instead of a real native app).
Capacitor supports Cordova Plugin. So it’s easy to migrate your Cordova application to Capacitor application. To add Capacitor into your front-end application, you can simply go to some app that you have and run the following command:
After installing it, you will have to initialize Capacitor by running this:
Then, you need to add the platform for which you want to build the application. For example, to configure your project to run on Android, you can issue the following command:
Finally, you need to open the project and run
or build
it via platform’s provide IDE. Run the following command to open Android Studio:
After that, you can use respective platform features to build or run the application.
Note: It’s also worth mentioning that you can combine Ionic, Stencil, and Capacitor to create a mobile application. Just follow the above instructions inside your Stencil Ionic PWA app, and you are good to go.
Ionic 4 In Action
In this section, you are going to create a very simple IonicaApplication with a couple of pages. What will be interesting about this app is that some of the content will be publicly available (that is, no authenticated is required) and some are for authenticated users only.
The application that you will build is similar to a contact directory. The first page will have a list of people with their names and avatar picture. Then, when users click on contact, the app will show the details of the contact clicked.
Creating an Auth0 Account
Before you move on and start developing the application, you will need to sign up to Auth0. The idea of using Auth0 is to leverage some state-of-the-art functionality that will give you enough power (and confidence) to focus on what matters the most, your app details. As such, the first thing you will do is to sign up for a free Auth0 account here .
After signing up to your Auth0 account, you will need to create an Auth0 Application to represent your new app. So, move to the Applications page in your Auth0 dashboard and click on Create Application. After clicking on it, Auth0 will show a dialog where you will have to type the name of your application (e.g., “Ionic 4 In Action”) and you will have to select the type of application (in this case, you will have to choose Native App). Then, you can press the Create button.
Clicking on this button will redirect you to the configuration details of your app. In this page, you will have to add the following values to the Allowed Web Origins box:
This makes Auth0 accept requests from this URLs. Alongside with that, you will also need to configure the callback URL for the application:
This is the URL that Auth0 will call after users authenticate.
Scaffolding the Ionic Application
First, you need to have the latest Ionic CLI installed, as mentioned earlier. If you haven’t done so yet, run the following command:
Now, run the following command to create a new blank project and to move into the project folder:
The first command will ask you a couple of questions:
- Integrate your new app with Cordova to target native iOS and Android? (y/N): Press Y
- Install the free Ionic Pro SDK and connect your app? (Y/n): Press N
Inside your new project, issue the following command to install the auth0-js
library:
Also install a CSS library to show some country flags, as follows:
After that, run the default app using Ionic CLI:
CLI will spin-up the web server and open this app in your default browser. If you take a closer look, you will see that Ionic CLI under the hood is using Angular CLI to run this app.
Creating a Service for your Data
Now that you have scaffolded your Ionic app, you will create a service called DataService
to fetch dummy data to your app. To create this service, run the following command from the project root:
This will generate a couple of files in the ./src/app/services/data
directory. Now, open the data.service.ts
file and replace its content with the following:
As you can see, this service is using an online API called Random User Generator to load a list of dummy contacts to your app. This service contains two methods:
getPeopleList
: The method that loads the dummy data and returns a list of 20 random contacts.getPerson
: A method that returns the details of a particular contact.
After creating this service, you will have to create another one. This other service will be responsible for handling your users’ sessions and, as such, you will call it AuthService
. To create this service, issue the following command:
Again, this command will generate a couple of files in the /src/app/services/auth
directory. Now, open the auth.service.ts
file and replace its code with the following:
Note that, before running your app again, you will have to replace YOUR_CLIENT_ID
and both places that show YOUR_AUTH_DOMAIN
with your own Auth0 values. The first value, you will have to replace with the Client ID property of the Auth0 Application you created before. The second value, YOUR_AUTH_DOMAIN
, you will have to replace with the Domain value of the same Auth0 Application.
Besides configuring your app to communicate with Auth0, this service also contains the following methods:
login
: This method initiates the login process and shows an Auth0’s hosted login page.handleAuthentication
: This method is called when the authentication process is finished. If the user is successfully logged in, this method callsthis.setSession
so it setsthis.isloggedIn
totrue
and redirect users to the/home
route. If the authentication is not successful, it setsthis.isloggedIn
tofalse
but also redirects to the/home
route. You will have to call this method inside the root component of your application so that it can read the URL hash using Auth0 library to determine the authentication status.setSession
: This method stores user’s login information likeaccess_token
,id_token
, andexpires_at
into local storage to facilitate the management of these data.logout
: This method removes all information about logged in user from local storage and setsthis.loggedin
value tofalse
.isAuthenticated
: This method checks if the user is authenticated or not by checking thetoken
expiration date from local storage.
Creating Pipes in Ionic 4
To see pipes in action, you are going to create a very simple pipe to capitalize the first character of words. As such, the first thing you will need to do is to create the pipe:
The above command will generate a file named capitalize.pipe.ts
inside the ./src/app/pipes/
directory. Open this file and replace its content with this:
The capitalize
method takes a string as an input and returns another string by capitalizing the first character of that string. When you use capitalize
pipe inside Angular Template, it will call the transform method, which will further the call to this capitalize
method from this pipe.
As you will need to use pipes in multiple sub-modules inside your application, it’s a good idea to create an extra NgModule
for all the pipes and then import those modules in your other modules. To do so, create a file named pipes.module.ts
inside ./src/app/pipes/
directory and add the following code to it:
Creating Pages in Ionic 4
By Default, the blank ionic project has only one page called HomePage
. To avoid creating a new one and removing this one, you will use that page to show the list of people.
So, replace the content of the ./src/app/home/home.page.ts
with the following:
Then, replace the content of the ./src/app/home/home.page.html
file with the following:
Since each page in Ionic is lazily loaded by default, each page comes with its own NgModule
. As such, you need to import the AppPipesModule
you just created in your ./src/app/home/home.module.ts
file, as shown below:
When users open your application, the app will load the home page. This homepage shows a list of people with their names and avatar. Users can click on anyone from the list and, clicking on any item, will trigger the openPerson
method from the HomePage
component. This will make your app load the /person
route with that person’s id
as a query parameter.
As such, you will also need to create a new page to show the details of a particular person. You can call this new page as PersonPage
and create it as follows:
This will generate multiple files related to the PersonPage
inside the ./src/app/person/
directory. Now, open the ./src/app/person/person.page.ts
and replace its contents with this:
Also, replace the contents of the ./src/app/person/person.page.html
file with the following:
Although the code of this new page is lengthy, you can see that it is not making anything too complex. When users click on someone from the list of people, the will open the PersonPage
. Then, on this page, users will be able to see information about that person. Some information is hidden and can be seen after logging in ([hidden]="!authService.isLoggedIn"
).
The last thing you will need to do in relation to your PersonPage
is to import the AppPipesModule
you created before in the ./src/app/person/person.module.ts
file, as shown below:
Importing the Flag Icon Library
Since you want to show some nice country flags alongside with each person’s detail, you will have to import this library inside your global.scss
file. To do so, open this file and add the following line into it:
Handling Authentication
After authentication requests are processed, Auth0 redirects users back to your application. Alongside with the user, it sends back authentication data via URL hashes. You need to parse that data and save it somewhere. As you probably remember, there is a method called handleAuthentication
in the AuthService
to do this. Now, you just have to call this method inside the constructor of app.component.ts
, as shown below:
Now, whenever you start a new project with Ionic CLI, it generates a module called app-routing.module.ts
for handling routing in your app. This file contains all the routing configuration. When you generate a page, CLI also adds that page to the routing configuration in this file. As such, you should have following in the app-routing.module.ts
file:
To make your PersonPage
support query parameters, modify the path: 'person'
route in the above file, as follows:
Then, to wrap things up, you will have to update the app.module.ts
file (which is located inside the /src/app/
directory) to tie everything together. In this file, you will need to add all the services, pipes, and pages that you created into it so that Angular can recognize them:
Now you can run the application as follows:
Note: If you get an error like, CapitalizePipe is declared in two modules, it’s because, when you create a pipe using Ionic CLI, it also adds that pipe inside the
app.module.ts
file. In this case, you have to removeCapitalizePipe
from theapp.module.ts
file.
Now, if you open the app in your browser (which actually Ionic does by default for you), you will see the following screen:
Conclusion
In this post, you learned about what’s new in Ionic 4, changes from Ionic 3, and how to migrate to the new version. After that, you learned about using Stencil and Capacitor along with Ionic to create mobile applications and, finally, you created and secured a simple Ionic 4 app. To learn more about Ionic 4, check out the official Ionic 4 documentation.