TypeError: Cannot assign to read only property
This tutorial guides you on how to resolve Error TypeError: Cannot assign to read only property ‘propertyX’ of object ‘[object Object]’. I was trying simple example myself on Components and Databinding and while binding to Custom Events I had stuck with this error.
TypeError: Cannot assign to read only property
core.js:4127 ERROR TypeError: Cannot assign to read only property 'itemAdded' of object '[object Object]' at new CreateItemComponent (create-item.component.ts:10) at NodeInjectorFactory.CreateItemComponent_Factory [as factory] (ɵfac.js? [sm]:1) at getNodeInjectable (core.js:3956) at instantiateAllDirectives (core.js:8417) at createDirectivesInstances (core.js:7784) at ɵɵelementStart (core.js:14528) at AppComponent_Template (template.html:2) at executeTemplate (core.js:7757) at renderView (core.js:7566) at renderComponent (core.js:8819)
Angular provides a way for you to emit custom events from the components if you use EventEmitter in components along with @Output directive as shown below. @Output decorator marks a class field as an output property.
The mistake I did was when I declared the class field as an output property, I missed parenthesis “()” for @Output decorator. The parenthesis is important because it needs to be executed like a function. Also make sure that “Output” is imported from “@angular/core“.
CreateItemComponent (Child Component) class
import { Component, OnInit, EventEmitter, Output } from '@angular/core'; @Component({ selector: 'app-create-item', templateUrl: './create-item.component.html', styleUrls: ['./create-item.component.css'] }) export class CreateItemComponent implements OnInit { @Output itemAdded = new EventEmitter<{itemTitle: string, itemDesc: string}>(); @Output itemSpecAdded = new EventEmitter<{itemTitle: string, itemSpec: string}>(); newItemName = ''; newItemDesc = ''; newItemSpec = ''; constructor() { } ngOnInit(): void { } onAddItem() { this.itemAdded.emit({ itemTitle: this.newItemName, itemDesc: this.newItemDesc }); } onAddItemSpec() { this.itemSpecAdded.emit({ itemTitle: this.newItemName, itemSpec: this.newItemSpec }); } }
Basically, I had to fix the following properties defined in the CreateItemComponent (child component) class as shown below. I just added parenthesis for the @Output directive.
@Output() itemAdded = new EventEmitter<{itemTitle: string, itemDesc: string}>(); @Output() itemSpecAdded = new EventEmitter<{itemTitle: string, itemSpec: string}>();
Actually, by using @Output directive we are passing data or our own event out of the component.
AppComponent (Parent Component) Template
Note, the DOM properties in the parent component’s template are bound to these output properties that is defined in the child component. The below is the code sneppet from AppComponent (parent component).
<div class="container"> <app-create-item (itemAdded)="onItemAdded($event)" (itemSpecAdded)="onItemSpecAdded($event)"></app-create-item> ---- ---- </div>
Actually, here we are listening for the custom events in the parent component emitted from the child component by binding custom events using property binding.
That’s it. After fixing the way decorators are declared, the error TypeError: Cannot assign to read only property ‘propertyx’ of object ‘[object Object]’ has disappeared.
Hope it helped 🙂
Also See:
- How to Pass data from child to parent component – Angular 9 ?
- Difference between declarations, providers, bootstrap and imports in @NgModule ?
- Pass variable from parent to custom child component – Angular 9 ?
- Can’t bind to ‘itemElem’ since it isn’t a known property of
- Best way to bundle an angular app for production deployment ?
- Type cannot be used as an index type.
- How to Modularize Angular Application – Angular 9 ?
- Declare model class and use in Angular component : Typescript
- Set default value in the dropdown list in Angular 9
- Best way to delete components in Angular 9 with CLI ?
- ERROR in multi /bootstrap.min.css ./src/styles.css in Angular 9
- Get index of ngFor element using index as value in attribute
- Type definition for properties and object literal in Typescript
- Apply CSS style attribute dynamically with ngStyle : Angular 9
- This version of CLI is only compatible with Angular versions
- Global Angular CLI version is greater than your local version
- Display different colors for odd/even rows using ngFor loop and ngClass
- TrackBy with *ngFor in Angular 9 : Example
- Differences between ViewEncapsulation.Native, ViewEncapsulation.None and ViewEncapsulation.Emulated