gpt4 book ai didi

Angular2 @Viewchild 单元测试

转载 作者:太空狗 更新时间:2023-10-29 17:15:51 27 4
gpt4 key购买 nike

我有这样一个组件:

export class ParentComponent implements OnInit, OnDestroy {


@ViewChild(ChildComponent) childComponent: ChildComponent;
}

它正在使用 childComponent 进行调用,我们这样说:

this.childComponent.method();

ParentComponent 方法中。

因此,当我尝试测试内部使用 ChildComponentParentComponent 方法时,childComponent 返回为未定义。

如何解决问题?

最佳答案

在 describe 中为子组件声明 fixture。

let childFixture: ComponentFixture<childComponent>;

现在使用

创建组件实例
let childComp = childFixture.componentInstance;

关于Angular2 @Viewchild 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45583959/

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