加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – 修改TestBed.overrideComponent中定义的组件

发布时间:2020-12-17 07:46:54 所属栏目:安全 来源:网络整理
导读:我们正在使用 TestBed.overrideComponent(CoolComponent,{ set: { template: 'div id="fake-component"i am the fake component/div',selector: 'our-cool-component',inputs: [ 'model' ] } }) 覆盖组件. 该组件具有我们在ngOnInit方法中配置的ViewChild @C
我们正在使用
TestBed.overrideComponent(CoolComponent,{
    set: {
      template: '<div id="fake-component">i am the fake component</div>',selector: 'our-cool-component',inputs: [ 'model' ]
    }
  })

覆盖组件.

该组件具有我们在ngOnInit方法中配置的ViewChild

@Component({
  selector: 'our-cool-component',templateUrl: 'cool.component.html'
})
export class CoolComponent implements OnInit,OnDestroy {
  @Input() model: SomeModel
  @ViewChild(CoolChildComponent) coolChildComponent;

  ngOnInit() {
    this.coolChildComponent.doStuff();
  }
}

CoolComponent依次生活在一个Wrapper组件中.

当我们在Wrapper fixture上调用fixture.detectChanges()时,会尝试构造CoolComponent,但是当它调用doStuff()时,它会立即死机,因为CoolChildComponent是未定义的.

有没有办法得到CoolComponent存根的CoolChildComponent?看起来我们可以把它从Wrapper中取消,因为它只能通过模板引用,而不是组件的属性.

ngOnInit() {
  this.coolChildComponent.doStuff();
}

应该

ngAfterViewInit() {
  this.coolChildComponent.doStuff();
}

because

View child is set before the ngAfterViewInit callback is called.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读