在下面的代码中,我将模板元素引用分配给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);
}
}
错误:预期进行分配
答案 0 :(得分:3)
您尝试将对象设置为等于视图容器的那一行看起来很糟糕。
使用键值对创建对象。但是你只提供了一个价值。
尝试这个(我不确定你为什么要这样做,但我想如果你要这样做,你必须做这样的事情):
@ViewChild('container') myContainer;
myObject = {
container : this.myContainer
}