17.10.2018 Views

Angular

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

]<br />

...<br />

},<br />

{<br />

},<br />

...<br />

path: 'users',<br />

component: UsersComponent,<br />

resolve: {<br />

// hence data.users in UsersComponent, note the pluralisation<br />

users: UsersResolver<br />

}<br />

app.resolver.ts<br />

Optionally bundle multiple resolvers together.<br />

IMPORTANT: Services used in resolver must be imported first or you will get a 'No provider for ..Resolver error'.<br />

Remember that these services will be available globally and you will not need to declare them in any component's<br />

providers anymore. Be sure to unsubscribe from any subscription to prevent memory leak<br />

...<br />

import { UsersService } from './users.service';<br />

import { UsersResolver } from './users.resolver';<br />

export const ROUTE_RESOLVERS = [<br />

...,<br />

UsersService,<br />

UsersResolver<br />

]<br />

main.browser.ts<br />

Resolvers have to be injected during bootstrapping.<br />

...<br />

import {bootstrap} from '@angular/platform-browser-dynamic';<br />

import { ROUTE_RESOLVERS } from './app.resolver';<br />

bootstrap(App, [<br />

...<br />

...ROUTE_RESOLVERS<br />

])<br />

.catch(err => console.error(err));<br />

Section 16.2: Routing with Children<br />

Contrary to original documentation, I found this to be the way to properly nest children routes inside the<br />

app.routing.ts or app.module.ts file (depending on your preference). This approach works when using either<br />

WebPack or SystemJS.<br />

The example below shows routes for home, home/counter, and home/counter/fetch-data. The first and last routes<br />

being examples of redirects. Finally at the end of the example is a proper way to export the Route to be imported in<br />

a separate file. For ex. app.module.ts<br />

To further explain, <strong>Angular</strong> requires that you have a pathless route in the children array that includes the parent<br />

component, to represent the parent route. It's a little confusing but if you think about a blank URL for a child route,<br />

GoalKicker.com – <strong>Angular</strong> 2 Notes for Professionals 74

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!