Blog service

Remember that we mentioned how having the business logic of our app in components is a bad idea. As much as possible, it's not recommended to interact with the data source directly from the component. What we will rather do is create a service class to do the same for us:

ng generate service blog

Update the generated empty service with the following:

import { Injectable } from '@angular/core';import * as rawData from './db.json';@Injectable()export class BlogService {  data = <any>rawData;  constructor() { }  getPosts() {    return => {      return {        id: post.imageId,        imageUrl: `,q_auto,w_300/${post.imageId}`,        author: post.collector      }    })  }  byId(id) { return ...

Get TypeScript 2.x for Angular Developers now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.