Typescript Destructuring Assignment for Global Variables

I have the following statement:

I get an object from somewhere like the following in the example:

 paginationConfig = { 'currentPage': 1, 'pageSizes': [5, 10, 15], 'perPage': 20 }; 

My class:

 export class MyComponent implements OnInit { currentPage: number; pageSizes: number[]; perPage: number; constructor() { } ngOnInit(): void { { this.currentPage, this.perPage, this.pageSizes } = paginationConfig; // It prints undefined for all those variables and I receive the error: // TS1128: Declaration or statement expected. // It works for local variables (as below), why not when I use **this**? const { currentPage, perPage, pageSizes } = paginationConfig; } } 

So, what I'm trying to do is destroy the object as examples in this book.

I can make it work if I use local variables, but in my case I really want them to be global. Is it impossible / not supported?

PS: I already tried to wrap everything in brackets, it also does not work.

+5
source share
1 answer

Use Object.assign from your class:

 Object.assign(this, paginationConfig); 
+3
source

Source: https://habr.com/ru/post/1264222/


All Articles