gpt4 book ai didi

Angular:@Input() 必须在@ViewChild 之上吗?为什么?

转载 作者:太空狗 更新时间:2023-10-29 18:18:50 26 4
gpt4 key购买 nike

我有一个带有@Input 值和@ViewChild 值的组件ImageUploaderComponent。

export class ImageUploaderComponent implements OnInit {
@Input() canvasheight: number;
@ViewChild('cropper', undefined)
...
}

如果 @Input() 行在 @ViewChild(...) 行之前,该组件工作正常,但如果它们被颠倒则不起作用。我认为这可能与 @ViewChild 没有使用分号有关,这会导致错误。

编辑:好吧,这是一个语法错误。与问题所暗示的相反,您不必在 @ViewChild 之上有 @Input()。

最佳答案

Input 和 ViewChild 装饰器没有特定的声明顺序。

请注意,变量仅在 ngOnInit() 中可用,而不是之前(比如在构造函数中)。

@ViewChild 声明中的“未定义”是什么?正确的声明是:

@ViewChild(ChildDirective) child: ChildDirective;

@ViewChild('reference') child: ChildDirective;

关于Angular:@Input() 必须在@ViewChild 之上吗?为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50014522/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com