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 gelöst: Type 'null' is not assignable to type 'string'.ts(2322)

11.02.2023 (👁2945)


 

Fehlermeldung Angular/TypeScript

(property) UserComponent.email: string

Type 'string | null' is not assignable to type 'string'.
Type 'null' is not assignable to type 'string'.ts(2322)

 

In der Angular Codezeile

ngOnInit() {

    this.email = localStorage.getItem('e');

  }

 

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

Automatisch generierte Beschreibung

 

Lösung: (ist einfach ein Trick)

Am Ende der Anweisung ein Ausrufezeichen setzen. Das bedeuted, es wird nur ausgeführt, wenn der rechte Wert nicht Null ist

ngOnInit() {

    this.email = localStorage.getItem('e')!;

  }

Ein Bild, das Text enthält.

Automatisch generierte Beschreibung