【发布时间】:2021-03-08 02:58:28
【问题描述】:
我将我的应用从 Angular v10 升级到 v11,现在我收到多个错误和警告,因为某些(可能是所有)字段可能一次未定义:
例如:
const savedToken = new Token(JSON.parse(localStorage.getItem(AuthInterceptor.tokenKey)));
必须变成:
const savedToken = new Token(JSON.parse(<string>localStorage.getItem(AuthInterceptor.tokenKey)));
警告:
Argument of type 'string | null' is not assignable to parameter of type 'string'.
Type 'null' is not assignable to type 'string'
// I know the localStorage value can be null in this case.
另一个例子:
public user$: Observable<User>;
// Property 'user$' has no initializer and is not definitely assigned in the constructor.
因为user$ 在ngOnInit() 中被初始化为this.user$ = this.store.pipe()...
你是怎么解决的?
即使我从angular.json 停用strict,结果也是一样的:
"@schematics/angular:application": {
"strict": false
}
我真的需要将我所有的应用程序代码逐行更新为 angular 11?谢谢
【问题讨论】:
标签: angular typescript strict