New power of Angular2 Router

42
New power of Angular2 Router Zhentian Wan

Transcript of New power of Angular2 Router

Page 1: New power of Angular2 Router

New power of Angular2 RouterZhentian Wan

Page 2: New power of Angular2 Router

Table of content

● Why need router?● About @NgModule● Router Basic● Lazy loading● Preload● Guards● Auxiliary router (named router)

Page 3: New power of Angular2 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

Page 4: New power of Angular2 Router

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.

Page 5: New power of Angular2 Router

Router in Angular2

@angular/router V3.2.4

Page 6: New power of Angular2 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

Page 7: New power of Angular2 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

Page 8: New power of Angular2 Router

About @NgModule -- Define a feature module

Page 9: New power of Angular2 Router

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

Page 10: New power of Angular2 Router

Router Basic -- Define routes

Page 11: New power of Angular2 Router

Router Basic -- Register root router

Page 12: New power of Angular2 Router

Router Basic -- Navigate to link

Page 13: New power of Angular2 Router

Router Basic -- Rendering the page

<router-outlet></router-outlet>

Page 14: New power of Angular2 Router

Lazy Loading

Core angularjs doesn’t (will NOT) support lazy loading with router!

UI Router + ocLazyLoad

+ webpack

Page 15: New power of Angular2 Router

Lazy Loading

Core angularjs doesn’t (will NOT) support lazy loading with router!

UI Router + ocLazyLoad Easy to use, Easy to configure

+ webpack

Page 16: New power of Angular2 Router

Lazy loading

Change

component

to

loadChildren

From:

Page 17: New power of Angular2 Router

Lazy loading

Change

component

to

loadChildren

Change To:

Page 18: New power of Angular2 Router

Lazy loading DEMO

DEMO

Page 19: New power of Angular2 Router

Preload

Proload lazy loaded module, from @angular/router V3.1.0

● Improve user experience● Load in background after main thread finish loading

Page 20: New power of Angular2 Router

Preload

● Preload All Modules● Customize preload modules

Page 22: New power of Angular2 Router

Preload -- Preload all modules

Page 23: New power of Angular2 Router

Preload -- Customize preload modules

Page 24: New power of Angular2 Router

Preload -- Customize preload modules

Page 25: New power of Angular2 Router

Preload -- Customize preload modules

Page 26: New power of Angular2 Router

Preload DEMO

DEMO

Page 27: New power of Angular2 Router

Guards

UI Router:

● Resolve ○ Auth○ Data○ redirection

Angular2 Router:

● CanActive● CanLoad● CanDeactive● Resolver (not really belongs to

guards)

Page 28: New power of Angular2 Router

Guards DEMO

DEMO

Page 29: New power of Angular2 Router

Auxiliary Router

● Multiple outlets, but only one url

Page 30: New power of Angular2 Router

Auxiliary Router

● Multiple outlets, but only one url● Divide browser into multi small windows, each with its own separate url

Page 31: New power of Angular2 Router

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

Page 32: New power of Angular2 Router

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

Page 33: New power of Angular2 Router

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!

Page 34: New power of Angular2 Router

UI Router -- Named views

Similar to UI Router Named views:

Page 35: New power of Angular2 Router

UI Router -- Named views

Similar to UI Router Named views:

Page 36: New power of Angular2 Router

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)

Page 37: New power of Angular2 Router

Auxiliary Router

Divide browser into multi small windows, each with its own separate url

Github

Page 38: New power of Angular2 Router

Auxiliary Router

Page 39: New power of Angular2 Router

Auxiliary router DEMO

DEMO

Page 41: New power of Angular2 Router

Questions?

Page 42: New power of Angular2 Router

Thanks!

Twitter: @ZhentiwEmail: [email protected]