This is just an Angular Service that contains methods of the problems that I have encountered during development.
- Angular ^4.0.0
- Underscore.js
Underscore.js
- Install underscorejs in your library
$ npm install underscore --save
$ npm install @types/underscore --save
- Edit
tsconfig.app.json
and add underscore to arraytypes
"types": [
"underscore"
]
- Import
HelperService
in your Component
import { HelperService } from 'location/helper.service';
- Add
HelperService
as a provider
@Component({
...
providers: [HelperService]
})
- Add
HelperService
in the constructor as private parameter
constructor(
private helper: HelperService
) { }
getRouteData(Router, fn): void
A subscriber of
router.events
that gets the data of the activated route. Data is passed through the callback, as well as the instanced Subscriber of the event.
Route:
{
path: '/home',
component: HomeComponent,
data: {foo: 'foo', bar: 'bar'}
}
Component:
routerSubscriber: any;
constructor(
private router: Router,
private helper: HelperService
) {
helper.getRouteData(router, (routeData, subscriber) => {
console.log(routeData.foo);
console.log(routeData.bar);
this.routerSubscriber = subscriber;
});
}
ngOnDestroy() {
this.routerSubscriber.unsubscribe();
}