我有以下声明:
我从某处接收到一个对象
,如下例所示:
paginationConfig = {
currentPage: 1,
pageSizes: [5, 10, 15],
perPage: 20
};
我的类(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, pageSizes, perPage } = paginationConfig;
}
}
所以我想做的是像 this 中的示例一样解构 object
书。
如果我使用local 变量,我可以让它工作,但在我的例子中,我真的希望它们是全局的。不可能/不支持吗?
PS:我已经尝试过将所有内容括在括号中,它也不起作用。
最佳答案
在你的类中使用 Object.assign
:
Object.assign(this, paginationConfig);
关于angular - Typescript 中全局变量的解构赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42239716/