【问题标题】:Are @Input() readonly good practice in Angular?@Input() 是 Angular 中只读的好习惯吗?
【发布时间】:2022-06-28 22:07:55
【问题描述】:

在 Angular 的 @Input() 字段上使用关键字 readonly 是否被认为是一种好习惯?

或者有什么缺点?

【问题讨论】:

    标签: angular


    【解决方案1】:

    您将无法使用@Input() 字段。

    Error: src/app/app.component.html:6:12 - error TS2540: Cannot assign to 'foo' because it is a read-only property.
    
    6 <app-test [foo]="5"></app-test>
             ~~~
    

    您可以通过删除来解决此错误

    "strictInputAccessModifiers": true
    

    【讨论】:

    • 我没有收到这个错误,你知道哪个选项会触发这个错误吗? (我启用了strictTemplates
    • 运行ng build时没有报错?
    • 哦,我是"strictInputAccessModifiers": true
    • 完美,感谢提醒!
    猜你喜欢
    • 2020-08-25
    • 2019-03-06
    • 1970-01-01
    • 2016-01-03
    • 2016-06-27
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多