Angular 在jasmine测试中有可能获得模板引用变量吗?
例如,在my-components-list.html中,我有:Angular 在jasmine测试中有可能获得模板引用变量吗?,angular,jasmine,Angular,Jasmine,例如,在my-components-list.html中,我有: 我想在我的测试中得到它,比如: it('test',()=>{ const myComponent:HTMLElement=fixture.debugElement.query(By.someMethod(“#first”); const myInput:HTMLElement=fixture.debugElement.query(By.someMethod('#forth')); } 您可以像在普通角度代码中一样使用模板引用
我想在我的测试中得到它,比如:
it('test',()=>{
const myComponent:HTMLElement=fixture.debugElement.query(By.someMethod(“#first”);
const myInput:HTMLElement=fixture.debugElement.query(By.someMethod('#forth'));
}
您可以像在普通角度代码中一样使用模板引用变量。使用@ViewChild decorator将它们定义为组件类中的字段:
@ViewChild('first')first:ElementRef;
@ViewChild('second')second:ElementRef;
...
然后在测试中,您可以像访问普通组件字段一样访问它们:fixture.componentInstance.first
首先,我希望这个问题有一个动态解决方案,我可以通过引用变量名访问元素,但我越是考虑所描述的解决方案,我认为这是正确的方法。它更加类型安全,元素被封装为组件的一部分,而不是模板。有没有直接访问模板的方法jasmine中的变量不必在组件类HI中创建这些字段,如果此ilne
@ViewChild('first'):ElementRef;
在基类中,是否可以获取模板变量的引用?请为此提供一个工作链接?