如何在对象属性上使用装饰器

时间:2017-01-08 07:25:06

标签: angular

在下面的代码中,我将模板元素引用分配给container类的MyComponent属性。

class MyComponent {
  @ViewChild('container') container;
  log(){
    console.log(this.container);
  }
}

如何在对象上创建container属性(下面的代码给出了错误)

class MyComponent {
  myObject = {
     @ViewChild('container') container;
  }
  log(){
    console.log(this.myObject.container);
  }
}
  

错误:预期进行分配

1 个答案:

答案 0 :(得分:3)

您尝试将对象设置为等于视图容器的那一行看起来很糟糕。

使用键值对创建对象。但是你只提供了一个价值。

尝试这个(我不确定你为什么要这样做,但我想如果你要这样做,你必须做这样的事情):

@ViewChild('container') myContainer;
myObject = {
     container : this.myContainer
}