Skip to content

Add task in service

  • βœ”οΈ
    Create a function in the Task Service
    Create a function which add a new task to the TaskService based on the task object passed as a parameter.

Add a method to add a task

The TaskService will be responsible for managing the tasks list as planned. Let’s add a function to add a task to the list.

πŸŽ“ Instructions

  1. Update the src/app/task.service.ts file.

    task.service.ts
    import { Injectable } from '@angular/core';
    import { Task } from './models/task.model';
    import { v4 as uuid } from 'uuid';
    @Injectable({
    providedIn: 'root'
    })
    export class TaskService {
    tasks: Task[] = [
    {
    id: uuid(),
    title: 'Task 1',
    description: 'Description of task 1',
    createdAt: new Date()
    },
    {
    id: uuid(),
    title: 'Task 2',
    description: 'Description of task 2',
    createdAt: new Date()
    }
    ];
    addTask(task: Task): void {
    this.tasks.push({
    ...task,
    createdAt: new Date()
    });
    }
    }

This new function will be called from the TaskFormComponent to add a new task to the list.

βœ”οΈ What you learned

You created a function in the TaskService to isolate the Task logic inside the service.