0%

angular-data-service

Introduction

在Angular开发中经常要使用一些后台数据,下面是一个封装好的DataService服务,用于获取后台数据。数据来自:JSONPlaceholder

DataService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import {Injectable} from '@angular/core';

@Injectable({providedIn: 'root'})
export class DataService {
constructor() {
}

async fetchTodoById(id: number) {
const response = await fetch(`https://jsonplaceholder.typicode.com/todos/${id}`);
return await response.json();
}

async fetch200Todos() {
const response = await fetch(`https://jsonplaceholder.typicode.com/todos`);
return await response.json();
}
}

Data types

Todo

1
2
3
4
5
6
export interface Todo {
userId: number;
id: number;
title: string;
completed: boolean;
}