在Typescript中为全局变量解析赋值

时间:2017-02-15 02:31:41

标签: angular typescript

我有以下声明:

我从某个地方收到object,如下例所示:

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

我的课程:

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;
  }
}

所以我要做的就是像here书中的例子一样对object进行解构。

如果我使用本地变量,我可以使用它,但在我的情况下,我真的希望这些变量是全局的。是不是可能/支持?

PS:我已经尝试将所有内容包装在括号中,但它也不起作用。

1 个答案:

答案 0 :(得分:4)

在班级中使用Object.assign

Object.assign(this, paginationConfig);
相关问题