總體頁面是一個元件模板,有一個子元件已經聲明好並在父元件模板中使用,現在我想要動態地在子元件內部建立子元件,我試過這樣做:
getViewRef用於動態創建子元件 FileUploadEle雖然可以正確創建,但創建後的元素並不屬於PhoneComponent如圖:
getViewRef
FileUploadEle
PhoneComponent
變成了兄弟元素。 該如何做才能正確地創建父子級而不是兄弟級的。
业精于勤,荒于嬉;行成于思,毁于随。
已經可以了,使用template指令
沒明白你的具體需求,但是我覺得這樣做比較好,使用模板聲明的方式,而不是在js裡創建組件。
注意下面的
import {Component} from '@angular/core'; @Component({ selector: 'parent', template: ` <p> <h1>parent</h1> <ng-content></ng-content> </p> ` }) export class ParentComponent {} @Component({ selector: 'child', template: `<h1>child</h1>` }) export class ChildComponent {} @Component({ selector: 'demo', template: `<parent><child></child></
已經可以了,使用template指令

沒明白你的具體需求,但是我覺得這樣做比較好,使用模板聲明的方式,而不是在js裡創建組件。
注意下面的