angular - Typescript 中全局变量的解构赋值

标签 angular typescript

我有以下声明:

我从某处接收到一个对象,如下例所示:

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/

相关文章:

javascript - Angular2 beta 7 上的 HashLocationStrategy

angular - Ionic 4 Angular Back Button 到上一页而不是 root?

javascript - 如何从数组中设置firebase数据库中的数据?

javascript - promise 数组和 Promise.all()

typescript - 动态导入的返回类型是什么?

typescript - 整数(文字)联合类型,以 NaN 作为有效返回值

android - tns-platform-declarations 的 nativescript-carousel 错误

angular - 可观察到的错误消息 - 无法在订阅中捕获

javascript - 为什么 Typescript 期望 fetch( )'s ` body` 属性为 ReadableStream<Uint8Array>?

javascript - 使用 "this"关键字冲突