SlideShare a Scribd company logo
1 of 42
Download to read offline
New power of Angular2
Router
Zhentian Wan
Table of content
● Why need router?
● About @NgModule
● Router Basic
● Lazy loading
● Preload
● Guards
● Auxiliary router (named router)
Why need router?
● Separate different areas of the app
● Maintain the state in the app
● Protect areas of the app based on certain rules
Why need router?
The URL bar provides a huge advantage for web
applications over native applications. It allows you to
reference states, bookmark them, and share them with
your friends. In a well-behaved web application, any
application state transition results in a URL change, and
any URL change results in a state transition. In other
words, a URL is a serialized router state.
Router in Angular2
@angular/router V3.2.4
About @NgModule
NgModule is a way to organize your dependencies for the compiler and
dependency injection (DI).
Three main benefits:
● Ahead of Time compiler (AoT compiler)
● DI
● Lazy load module with Router
About @NgModule
NgModule is a way to organize your dependencies for the compiler and
dependency injection (DI).
Three main benefits:
● Ahead of Time compiler (AoT compiler)
● DI
● Lazy load module with Router
About @NgModule -- Define a feature
module
Router Basic
Three main components that use to configure routing:
● Routers describe the routes our application supports
● RouterOutlet is where page content goes into
● RouterLink directive is used to do navigation
Router Basic -- Define routes
Router Basic -- Register root router
Router Basic -- Navigate to link
Router Basic -- Rendering the page
<router-outlet></router-outlet>
Lazy Loading
Core angularjs doesn’t (will NOT) support lazy
loading with router!
UI Router + ocLazyLoad
+ webpack
Lazy Loading
Core angularjs doesn’t (will NOT) support lazy
loading with router!
UI Router + ocLazyLoad
Easy to use, Easy to configure
+ webpack
Lazy loading
Change
component
to
loadChildren
From:
Lazy loading
Change
component
to
loadChildren
Change To:
Lazy loading DEMO
DEMO
Preload
Proload lazy loaded module, from @angular/router V3.1.0
● Improve user experience
● Load in background after main thread finish loading
Preload
● Preload All Modules
● Customize preload modules
Preload
AngularConnect16
Preload -- Preload all modules
Preload -- Customize preload modules
Preload -- Customize preload modules
Preload -- Customize preload modules
Preload DEMO
DEMO
Guards
UI Router:
● Resolve
○ Auth
○ Data
○ redirection
Angular2 Router:
● CanActive
● CanLoad
● CanDeactive
● Resolver (not really belongs to
guards)
Guards DEMO
DEMO
Auxiliary Router
● Multiple outlets, but only one url
Auxiliary Router
● Multiple outlets, but only one url
● Divide browser into multi small windows, each with its own separate url
Auxiliary Router
● Multiple outlets, but only one url
● Divide browser into multi small windows, each with its own separate url
● Provide separate routing configuration for each url
Auxiliary Router
● Multiple outlets, but only one url
● Divide browser into multi small windows, each with its own separate url
● Provide separate routing configuration for each url
● Each window navigate separately
Auxiliary Router
● Multiple outlets, but only one url
● Divide browser into multi small windows, each with its own separate url
● Provide separate routing configuration for each url
● Each window navigate separately
Lots similar to UI Router named views, but away more powerful!
UI Router -- Named views
Similar to UI Router Named views:
UI Router -- Named views
Similar to UI Router Named views:
Auxiliary Router
● Multiple outlets, but only one url
● Divide browser into multi small windows,
each with its own separate url
● Provide separate routing configuration for
each url
● Each window navigate separately
localhost:4200/speakers/(list:speakersList//bio:none)
Auxiliary Router
Divide browser into multi small
windows, each with its own separate
url
Github
Auxiliary Router
Auxiliary router DEMO
DEMO
Resources:
DEMO: https://github.com/zhentian-wan/tamperejs-2016-dec-ng2router-demo
Articles:
http://blog.angular-university.io/angular2-router/
https://vsavkin.com/angular-2-router-d9e30599f9ea#.r2fpxy2cp
http://blog.thoughtram.io/angular/2016/06/14/routing-in-angular-2-revisited.html
Videos:
https://egghead.io/courses/intro-to-angular-2-router
https://www.youtube.com/watch?v=QLns6s02O48
Book:
https://leanpub.com/router
Questions?
Thanks!
Twitter: @Zhentiw
Email: answer881215@gmail.com

More Related Content

What's hot

Android application architecture
Android application architectureAndroid application architecture
Android application architectureRomain Rochegude
 
OpenCms Days 2016: Next generation content repository
OpenCms Days 2016: Next generation content repository OpenCms Days 2016: Next generation content repository
OpenCms Days 2016: Next generation content repository Alkacon Software GmbH & Co. KG
 
Enterprise(d) Tomcat & httpd
Enterprise(d) Tomcat & httpdEnterprise(d) Tomcat & httpd
Enterprise(d) Tomcat & httpdVaclav Tunka
 
02 java programming features of java i
02 java programming features of java  i02 java programming features of java  i
02 java programming features of java iRavi Patki
 
OpenCms Days 2014 - Enhancing OpenCms front end development with Sass and Grunt
OpenCms Days 2014 - Enhancing OpenCms front end development with Sass and GruntOpenCms Days 2014 - Enhancing OpenCms front end development with Sass and Grunt
OpenCms Days 2014 - Enhancing OpenCms front end development with Sass and GruntAlkacon Software GmbH & Co. KG
 

What's hot (8)

RoR app screencast
RoR app screencastRoR app screencast
RoR app screencast
 
Android application architecture
Android application architectureAndroid application architecture
Android application architecture
 
OpenCms Days 2015 Next generation repository
OpenCms Days 2015  Next generation repositoryOpenCms Days 2015  Next generation repository
OpenCms Days 2015 Next generation repository
 
Carbon and OSGi Deep Dive
Carbon and OSGi Deep DiveCarbon and OSGi Deep Dive
Carbon and OSGi Deep Dive
 
OpenCms Days 2016: Next generation content repository
OpenCms Days 2016: Next generation content repository OpenCms Days 2016: Next generation content repository
OpenCms Days 2016: Next generation content repository
 
Enterprise(d) Tomcat & httpd
Enterprise(d) Tomcat & httpdEnterprise(d) Tomcat & httpd
Enterprise(d) Tomcat & httpd
 
02 java programming features of java i
02 java programming features of java  i02 java programming features of java  i
02 java programming features of java i
 
OpenCms Days 2014 - Enhancing OpenCms front end development with Sass and Grunt
OpenCms Days 2014 - Enhancing OpenCms front end development with Sass and GruntOpenCms Days 2014 - Enhancing OpenCms front end development with Sass and Grunt
OpenCms Days 2014 - Enhancing OpenCms front end development with Sass and Grunt
 

Similar to New power of Angular2 Router

Android Modularization
Android ModularizationAndroid Modularization
Android ModularizationYoung-Hyuk Yoo
 
Meet Magento Spain 2019 - Our Experience with Magento Cloud
Meet Magento Spain 2019 - Our Experience with Magento CloudMeet Magento Spain 2019 - Our Experience with Magento Cloud
Meet Magento Spain 2019 - Our Experience with Magento CloudLyzun Oleksandr
 
JSFest 2019: Technology agnostic microservices at SPA frontend
JSFest 2019: Technology agnostic microservices at SPA frontendJSFest 2019: Technology agnostic microservices at SPA frontend
JSFest 2019: Technology agnostic microservices at SPA frontendVlad Fedosov
 
Deploy Eclipse hawBit in Production
Deploy Eclipse hawBit in ProductionDeploy Eclipse hawBit in Production
Deploy Eclipse hawBit in ProductionKynetics
 
JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...
JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...
JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...JSFestUA
 
Rails is Easy*
Rails is Easy*Rails is Easy*
Rails is Easy*bryanbibat
 
Lupus Decoupled Drupal - Drupal Austria Meetup - 2023-04.pdf
Lupus Decoupled Drupal - Drupal Austria Meetup - 2023-04.pdfLupus Decoupled Drupal - Drupal Austria Meetup - 2023-04.pdf
Lupus Decoupled Drupal - Drupal Austria Meetup - 2023-04.pdfWolfgangZiegler6
 
Decoupled drupal + vue.js
Decoupled drupal + vue.jsDecoupled drupal + vue.js
Decoupled drupal + vue.jsEugene Vozniuk
 
Browserify + Angular
Browserify + AngularBrowserify + Angular
Browserify + AngularBen Drucker
 
Angular js 1.3 presentation for fed nov 2014
Angular js 1.3 presentation for fed   nov 2014Angular js 1.3 presentation for fed   nov 2014
Angular js 1.3 presentation for fed nov 2014Sarah Hudson
 
What's coming in Airflow 2.0? - NYC Apache Airflow Meetup
What's coming in Airflow 2.0? - NYC Apache Airflow MeetupWhat's coming in Airflow 2.0? - NYC Apache Airflow Meetup
What's coming in Airflow 2.0? - NYC Apache Airflow MeetupKaxil Naik
 
Pluggable web app using Angular (Odessa JS conf)
Pluggable web app using Angular (Odessa JS conf)Pluggable web app using Angular (Odessa JS conf)
Pluggable web app using Angular (Odessa JS conf)Saif Jerbi
 
GeoServer Developers Workshop
GeoServer Developers WorkshopGeoServer Developers Workshop
GeoServer Developers WorkshopJody Garnett
 
Liferay portals in real projects
Liferay portals  in real projectsLiferay portals  in real projects
Liferay portals in real projectsIBACZ
 
Scaling AngularJS: Enterprise SOA on the MEAN Stack (Responsive Web & Mobile)
Scaling AngularJS: Enterprise SOA on the MEAN Stack (Responsive Web & Mobile)Scaling AngularJS: Enterprise SOA on the MEAN Stack (Responsive Web & Mobile)
Scaling AngularJS: Enterprise SOA on the MEAN Stack (Responsive Web & Mobile)Movel
 
Counterclockwise past present future
Counterclockwise  past present futureCounterclockwise  past present future
Counterclockwise past present futurelolopetit
 
Google I/O 2019 - what's new in Android Q and Jetpack
Google I/O 2019 - what's new in Android Q and JetpackGoogle I/O 2019 - what's new in Android Q and Jetpack
Google I/O 2019 - what's new in Android Q and JetpackSunita Singh
 
SCM Puppet: from an intro to the scaling
SCM Puppet: from an intro to the scalingSCM Puppet: from an intro to the scaling
SCM Puppet: from an intro to the scalingStanislav Osipov
 

Similar to New power of Angular2 Router (20)

ngconf2015
ngconf2015ngconf2015
ngconf2015
 
Android Modularization
Android ModularizationAndroid Modularization
Android Modularization
 
Meet Magento Spain 2019 - Our Experience with Magento Cloud
Meet Magento Spain 2019 - Our Experience with Magento CloudMeet Magento Spain 2019 - Our Experience with Magento Cloud
Meet Magento Spain 2019 - Our Experience with Magento Cloud
 
JSFest 2019: Technology agnostic microservices at SPA frontend
JSFest 2019: Technology agnostic microservices at SPA frontendJSFest 2019: Technology agnostic microservices at SPA frontend
JSFest 2019: Technology agnostic microservices at SPA frontend
 
Deploy Eclipse hawBit in Production
Deploy Eclipse hawBit in ProductionDeploy Eclipse hawBit in Production
Deploy Eclipse hawBit in Production
 
JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...
JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...
JS Fest 2019/Autumn. Влад Федосов. Technology agnostic microservices at SPA f...
 
Rails is Easy*
Rails is Easy*Rails is Easy*
Rails is Easy*
 
Lupus Decoupled Drupal - Drupal Austria Meetup - 2023-04.pdf
Lupus Decoupled Drupal - Drupal Austria Meetup - 2023-04.pdfLupus Decoupled Drupal - Drupal Austria Meetup - 2023-04.pdf
Lupus Decoupled Drupal - Drupal Austria Meetup - 2023-04.pdf
 
Decoupled drupal + vue.js
Decoupled drupal + vue.jsDecoupled drupal + vue.js
Decoupled drupal + vue.js
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Browserify + Angular
Browserify + AngularBrowserify + Angular
Browserify + Angular
 
Angular js 1.3 presentation for fed nov 2014
Angular js 1.3 presentation for fed   nov 2014Angular js 1.3 presentation for fed   nov 2014
Angular js 1.3 presentation for fed nov 2014
 
What's coming in Airflow 2.0? - NYC Apache Airflow Meetup
What's coming in Airflow 2.0? - NYC Apache Airflow MeetupWhat's coming in Airflow 2.0? - NYC Apache Airflow Meetup
What's coming in Airflow 2.0? - NYC Apache Airflow Meetup
 
Pluggable web app using Angular (Odessa JS conf)
Pluggable web app using Angular (Odessa JS conf)Pluggable web app using Angular (Odessa JS conf)
Pluggable web app using Angular (Odessa JS conf)
 
GeoServer Developers Workshop
GeoServer Developers WorkshopGeoServer Developers Workshop
GeoServer Developers Workshop
 
Liferay portals in real projects
Liferay portals  in real projectsLiferay portals  in real projects
Liferay portals in real projects
 
Scaling AngularJS: Enterprise SOA on the MEAN Stack (Responsive Web & Mobile)
Scaling AngularJS: Enterprise SOA on the MEAN Stack (Responsive Web & Mobile)Scaling AngularJS: Enterprise SOA on the MEAN Stack (Responsive Web & Mobile)
Scaling AngularJS: Enterprise SOA on the MEAN Stack (Responsive Web & Mobile)
 
Counterclockwise past present future
Counterclockwise  past present futureCounterclockwise  past present future
Counterclockwise past present future
 
Google I/O 2019 - what's new in Android Q and Jetpack
Google I/O 2019 - what's new in Android Q and JetpackGoogle I/O 2019 - what's new in Android Q and Jetpack
Google I/O 2019 - what's new in Android Q and Jetpack
 
SCM Puppet: from an intro to the scaling
SCM Puppet: from an intro to the scalingSCM Puppet: from an intro to the scaling
SCM Puppet: from an intro to the scaling
 

New power of Angular2 Router