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 this.data.map(post => {      return {        id: post.imageId,        imageUrl: `https://res.cloudinary.com/christekh/image/upload/c_fit,q_auto,w_300/${post.imageId}`,        author: post.collector      }    })  }  byId(id) { return this.data ...

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.