15

में परिभाषित हमको संशोधित करने के एक घटक TestBed.overrideComponent

TestBed.overrideComponent(CoolComponent, { 
    set: { 
     template: '<div id="fake-component">i am the fake component</div>', 
     selector: 'our-cool-component', 
     inputs: [ 'model' ] 
    } 
    }) 

उपयोग कर रहे हैं एक घटक ओवरराइड करने के लिए।

घटक एक ViewChild जो हम अपने ngOnInit विधि

@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.detectChanges() कहते हैं, इस CoolComponent का निर्माण करने का प्रयास करता है, लेकिन यह तुरंत मर जाता है जब यह doStuff() कहता है क्योंकि CoolChildComponent अनिर्धारित रहता है।

क्या पर अपने CoolChildComponent को रोकने के लिए कोई तरीका है? ऐसा प्रतीत नहीं होता है कि हम इसे Wrapper से निकाल सकते हैं क्योंकि यह केवल टेम्पलेट के माध्यम से संदर्भित है, घटक की संपत्ति के रूप में नहीं।

+1

मैं सिर्फ एक इनाम, जाहिरा तौर पर आपको सूचित नहीं कर रहे हैं (http://meta.stackoverflow.com/q/333838/3001761) – jonrsharpe

+0

यह कभी काम कर रहा है जोड़ा? क्या यह ब्राउज़र में चलाए जाने पर काम करता है (या जो भी आप लक्षित कर रहे हैं)? क्या यह एक परीक्षण केवल समस्या है? – j2L4e

उत्तर

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

होना चाहिए ngAfterViewInit कॉलबैक कहा जाता है से पहले

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

because

देखें बच्चा निर्धारित है।

+0

यह काम करता है? –

+0

नहीं, इससे मेरे लिए कोई फर्क नहीं पड़ता है। – conor

संबंधित मुद्दे

 संबंधित मुद्दे