javascript - 属性“filteredOptions”没有初始值设定项,也没有在构造函数中明确分配
问题描述
在我的 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
可能有也可能没有价值。在这种情况下,您有责任在您的情况下处理未定义的值(?.
在您的 HTML 模板中使用,null
并undefined
检查...等)
推荐阅读
- hyperledger-fabric - 允许参与者完全读取权限
- vba - 在特定条件下保持单元格不变
- angularjs - angularjs-ui-tinymce 绑定不起作用
- xml - 如何使用 XSLT 为每个 html 选择设置唯一名称?
- database - 将 AWS DynamoDB 迁移到 Postgresql
- c# - 检查 Textbox.Text 上的多个条件的最佳方法
- c# - ModelState 是有效的,但是当我保存数据时,我在有错误的 entityValidationErrors 中得到它
- sql - 如何获取上周(7天)每天的记录数oracle
- javascript - Facebook 页面插件 - 响应式
- php - 如何将子字符串添加到以逗号分隔的字符串中的每个元素