我不知道为什么简单的*ngIf="false"
会导致error TS2532: Object is possibly 'undefined'.
。
tsconfig.json
"compilerOptions": {
...
"strict": true,
export class AppComponent {
user? : User;
}
interface User {
name: string;
}
app.component.html
<div *ngIf="user">
<div>{{user.name}}</div> <!-- OK -->
<div *ngIf="true">{{user.name}}</div> <!-- OK -->
<div *ngIf="false">{{user.name}}</div> <!-- error TS2532: Object is possibly 'undefined'. -->
</div>