在我的 Angular 应用程序中,我有一个组件:
filteredOptions: Observable<string[]>;
以下行给出错误:Property 'filteredOptions' has no initializer and is not definitely assigned in the constructor.
28 filteredOptions: Observable<string[]>;
最佳答案
这是因为 typescript 2.7.2
包括一个严格的类检查,所有属性都应该在构造函数中声明。因此,要解决这个问题,您可以:
name!:string;
"strictPropertyInitialization": false
到您的编译器选项。 filteredOptions: Observable<string[]> | undefined;
filteredOptions?: Observable<string[]>
更多详情:
name!:string;
: 编译器相信我,我会保证它不会是未定义的(可能导致不可预知的行为)"strictPropertyInitialization": false
: 编译器请停止你的严格检查filteredOptions
可能有也可能没有值(value)。在这种情况下,您有责任在您的情况下处理未定义的值(在您的 HTML 模板中使用 ?.
,null
和 undefined
检查...等)关于javascript - 属性 'filteredOptions' 没有初始化器,也没有在构造函数中明确分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66315793/