我的模板中包含以下組件:<vector-editor #scaleControl [x]="scaleX" [y]="scaleY" [z]="scaleZ" > </vector-editor>該vector-editor結(jié)構(gòu)如下:export class VerticeControlComponent implements OnInit{ @Input() x: number; @Input() y: number; @Input() z: number; ...}在我的應(yīng)用程序中,我獲取了有關(guān)#scaleControl使用的參考@ViewChild('scaleControl') scaleControl: ElementRef;現(xiàn)在,如果我輸出scaleControl到控制臺,則會得到以下結(jié)果:可以看出,引用不是null,并且組件的所有屬性都在其中。我想訪問這些屬性,但是在代碼中,scaleControlis 的實(shí)際類型是ElementRef而不是VectorEditorControl在輸出中看到的。因此,我不允許TypeScript使用this.scaleControl.x。我也試過ElementRef像這樣var temp = <VectorEditorControl>this.scaleControl但我得到一個錯誤,告訴我,我不能投ElementRef來VectorEditorControl最后,我嘗試訪問,this.scaleControl.nativeElement但未定義...我在這里想念什么?
3 回答

幕布斯7119047
TA貢獻(xiàn)1794條經(jīng)驗 獲得超8個贊
在撰寫本文時,我建議使用以下方法:
@ViewChild('scaleControl') scaleControl: ElementRef<VerticeControlComponent>;
然后,您可以訪問這兩種類型。
ElementRef:使用scaleControl
VerticeControlComponent:使用scaleControl.nativeElement
- 3 回答
- 0 關(guān)注
- 1177 瀏覽
添加回答
舉報
0/150
提交
取消