import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; /* * We're loading this component asynchronously * We are using some magic with es6-promise-loader that will wrap the module with a Promise * see https://github.com/gdi2290/es6-promise-loader for more info */ console.log('`About` component loaded asynchronously'); @Component({ selector: 'about', template: `
npm run start:hmr
this.localState = {{ localState | json }}
`
})
export class AboutComponent implements OnInit {
localState: any;
constructor(public route: ActivatedRoute) {
}
ngOnInit() {
this.route
.data
.subscribe((data: any) => {
// your resolved data from route
this.localState = data.yourData;
});
console.log('hello `About` component');
// static data that is bundled
// var mockData = require('assets/mock-data/mock-data.json');
// console.log('mockData', mockData);
// if you're working with mock data you can also use http.get('assets/mock-data/mock-data.json')
this.asyncDataWithWebpack();
}
asyncDataWithWebpack() {
// you can also async load mock data with 'es6-promise-loader'
// you would do this if you don't want the mock-data bundled
// remember that 'es6-promise-loader' is a promise
setTimeout(() => {
System.import('../../../assets/mock-data/mock-data.json')
.then(json => {
console.log('async mockData', json);
this.localState = json;
});
});
}
}