Is it possible to use an observable without subscribing?

Discussão em 'Angular' iniciado por vinudev, Novembro 28, 2024.

  1. vinudev

    vinudev Guest

    Consulting articles and documentation, I saw that it is not possible to use an observable without subscribe, I have the following code in my service, why am I able to use the observable without subscribe? Wasn't it supposed to give an error?

    import { HttpClient, HttpHeaders } from '@angular/common/http';
    import { Injectable } from '@angular/core';
    import { Observable } from 'rxjs';
    import { User } from '../models/user';

    const httpOptions = {


    headers: new HttpHeaders({'content-Type': 'application/json'})
    }

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

    BASE_URL: string = 'http://localhost:3000/';


    constructor(private segurancaHttp: HttpClient) { }


    getUsers(): Observable<User[]>{
    return this.segurancaHttp.get<User[]>(this.BASE_URL + 'users')
    }

    addUser(user:any): Observable<User>{
    return this.segurancaHttp.post<User>(this.BASE_URL + 'users', user, httpOptions)
    }


    I'm studying observables and this is giving me a lot of doubts.

    Continue reading...

Compartilhe esta Página