How to use AngularJS Component Router with TypeScript?
AngularJS version 1.5 introduced Components that is very useful and provides the best of both controllers and directives. In this post, I’ll tell you how to use AngularJS Component Router with TypeScript that will help you to write better components. Make sure that you know the basics of using component before reading this post.
Scope
The scope of this post is to illustrate how to use ngRoute/ui-router module with component and component router with component. I’ll be reusing the component that I created in post linked above for the rest of this post. If you follow the previous and this post, you will have this end result.
Angular Component Router Demo
What is Routing?
Routing is a very important part of your web application as you can change a whole view or parts of a view based on changes in URL.
What are the router options available in Angular?
There are few options available when it comes to routing in Angular, namely: ngRoute, ui-router and component router. ngRoute module is available out of the box in the framework and ui-router is an 3rd party module. Component router was introduced in version 1.5.
How to use ngRoute/ui-router module with angular component?
If you are using ngRoute or ui-router module in your app and would like to use component, you can still activate a component based on a url hash fragment or a state. The trick is to use the template binding and render a component directive. I’ll be using the component from my previous post to illustrate this in code snippet below:
1 2 3 4 5 6 7 |
angular.module(‘app’, [‘ngRoute’]) .config(($routeProvider: angular.route.IRouteProvider) => { $routeProvider .when(‘/list’, { template: ‘<review-list></review-list>’ }) .when(‘/about’, { template: ‘<app-about></app-about>’ }) .otherwise({ redirectTo: ‘/list’ }); }); |
Make sure you have proper router module script added to your document. I’m using ngRoute above in a module named app. The snippet uses template property to specify component directive. reviewList is the component that I created in my previous post and appAbout is another easy component that I added just to illustrate routing. Its implementation is mentioned below and it just has a hard-coded content defined using template property:
1 2 3 4 5 6 7 8 9 |
namespace app.components { class AboutComponent implements ng.IComponentOptions { template = ‘This is about component’; } angular.module(‘app’).component(‘appAbout’, new AboutComponent()); } |
Above knowledge is useful when you app is already using a routing module and you want to combine it with the new angular components.
Component Router
Component Router is quite interesting. It is compatible with Angular 2. It also provides useful life-cycle hooks that I’ll explain later in this post. Even nested routing can be used with component router for which I’ll write another post soon. Nested routing is a concept that is not available in ngRoute module but is present in ui-router module. Another important concept is link generation that I’ll explain in this post.
To use any angular module, first requirement is to add script source. Component router can be found from this repository.
Next step is to add this module as a dependency to your own angular module. When this router module is injected, it knows that it needs to look at $routerRootComponent service to instruct about the top-level component that will have all the navigation rules as mentioned below:
1 2 |
angular.module(‘app’, [‘ngComponentRouter’]) .value(‘$routerRootComponent’, ‘reviewApp’); |
That also means, we need to write another component named reviewApp and includes the source file reference in the HTML document. I’ve created review-app.component.ts and review-app.component.html files. Implementation for both is mentioned below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
namespace app.components { class ReviewAppComponent implements ng.IComponentOptions { templateUrl = ‘/app/components/review-app.component.html’; $routeConfig = [ { path: ‘/list’, component: ‘reviewList’, name: ‘List’ }, { path: ‘/about’, component: ‘appAbout’, name: ‘About’ }, { path: ‘/details/:id’, component: ‘reviewDetails’, name: ‘Details’ }, { path: ‘/**’, redirectTo: [‘List’]} // array to pass params to a route, wildcard character to matching any other requested route ]; } angular.module(‘app’).component(‘reviewApp’, new ReviewAppComponent()); } |
The component above defines $routeConfig which is quite similar to defining routes using other route modules discussed above. For a matching path, we instruct it to render a component. We can also define an otherwise route so that if a requested route doesn’t exist, then redirect to some other route. name member in the definition is very important as it is used with redirectTo as well as for link generation – that I’ll explain soon.
Let us look at the html document source below that is linked with this component via a templateUrl. So, when this top-level component is requested, the above HTML is rendered.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<ul> <!—<li><a href=“#/list”>List</a></li> <li><a href=“#/about”>About</a></li>—> <li><a data–ng–link=“[‘List’]”>List</a></li> <!— is changed to <a data–ng–link=“[‘List’]” href=“./list”>List</a> —> <li><a data–ng–link=“[‘About’]”>About</a></li> <!— is changed to <a data–ng–link=“[‘About’]” href=“./about”>About</a> —> </ul> <!— similar to ngView —> <ng–outlet></ng–outlet> |
It shows to use anchor tags for routing with component router in two ways, using #/path and ng-link=['name']. The latter one is the concept of link generation where it matches the name member of routes. If one is found, it then uses the path member’s value to add href attribute to the anchor element as shown above.
Using ngLink directive to reference a route with its name is good so that if in case you change the path for your route, you will just need to change in the configuration and all the references will still use name member’s value. So, it is good for maintenance.
The other directive to note is ngOutlet which is similar to native ngView which works as a placeholder for other views.
Component Router with Parameters
Line no. 8 in the component definition above illustrates how to define a route that any parameter(s). The path is /details/:id which instructs that id will be a dynamic value. This syntax is also used in other router modules. As you can see it also consumes a new component named reviewDetails with a unique name.
Just to link reviewDetails component with our component from previous post, add a link to following to review-list.component.html source which can allow user to see details of a review: