Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Angular: .this vermeiden mit const

08.02.2023 (👁2677)

Angular: this vermeiden

 

 

Wieso muss ich immer this.xxx verwenden in einer Angular.component.ts oder service.ts

Wenn man die Variable IN die CLASS schreibt, dann muss man die Variable in den den Unterfunktionen mit this. wieder holen

Ein Bild, das Text enthält.

Automatisch generierte Beschreibung

import { HttpClient } from '@angular/common/http';

import { Injectable } from '@angular/core';

import { Observable } from 'rxjs';

import { environment } from 'src/environments/environment';

import { User } from '../models/user';

 

//const api_url = environment.apiUrl + "/Auth";

 

@Injectable({

  providedIn: 'root'

})

export class AuthService {

 

  //*opens webapi and gets data

  private api_url = environment.apiUrl + "/Auth";

 

  constructor(private http: HttpClient) { }  //*inject web-api caller

 

  public register(user: User): Observable<any> {

    return this.http.post<any>(

      this.api_url + "/register"

      //api_url+ "/register"

      ,user

      )

Wenn es globale Konstanten sind, dann man man sie ausserhalb der CLASS eintragen und dann direkt aufrufen

Ein Bild, das Text, Screenshot, Monitor, Bildschirm enthält.

Automatisch generierte Beschreibung

Beispiel

import { HttpClient } from '@angular/common/http';

import { Injectable } from '@angular/core';

import { Observable } from 'rxjs';

import { environment } from 'src/environments/environment';

import { User } from '../models/user';

 

const url_Api_Auth = environment.apiUrl + "/Auth";

 

@Injectable({

  providedIn: 'root'

})

export class AuthService {

 

  //*opens webapi and gets data

  //private api_url = environment.apiUrl + "/Auth";

 

  constructor(private http: HttpClient) { }  //*inject web-api caller

 

  public register(user: User): Observable<any> {

    return this.http.post<any>(

      //this.api_url + "/register"

      url_Api_Auth+ "/register"

      ,user

      )

  }