My goal is to create a component property that is read-only. However, I am facing an issue where the readonly modifier does not seem to have any effect.
According to the documentation, once I initialize the cars
property in the constructor()
of AppComponent, it should not be modifiable.
What I have attempted:
Initially, I was able to modify the cars
property in AppComponent's ngOnInit()
. I assumed this might be allowed as the component is still under construction. To test further, I created a button that calls a function to modify the property and found that I could do so again. This led me to think that maybe the readonly modifier only applies when accessed by external classes. However, even in HelloComponent, I could still modify cars
.
Am I misunderstanding how readonly works? How can I correctly implement it to make a property accessible publicly but non-modifiable (read-only) from outside sources?