Cannot assign to read only property 'name' of object '[object Object]'

后端 未结 6 1535
攒了一身酷
攒了一身酷 2021-02-04 03:19

The following code will throw an error only for the name property. It could be fixed by specifying name property as writable in Object.create

6条回答
  •  情深已故
    2021-02-04 03:57

    If you get this error in Angular+Typescript+NgRX:

    You can use the spread operator to take a shallow copy of a readonly object to make it readable, however you may not want this depending on your situation.

    let x = [...y];
    

    If you're using Redux / NgRX, there's a chance your selector could be returning a readonly object with a reference to the store, which can throw exceptions when trying to alter that object property via template binding. Depending on your situation, you can take a deep copy to remove the store reference.

    let x = JSON.parse(JSON.stringify(y));
    

提交回复
热议问题