angular2 call function of parent component

前端 未结 4 844
有刺的猬
有刺的猬 2021-01-30 01:33

I have an app where I have an upload component where I can upload a file. It is embedded in the body.component.

On upload, it should use a function (e.g.

4条回答
  •  失恋的感觉
    2021-01-30 01:47

    You can inject the parent component to the child component.

    For more details see
    - How do I inject a parent component into a child component?
    - Angular 2 child component refers to parent component This way you can ensure that thefunction() is only called when parent is a body.component.

    constructor(@Host() bodyComp: BodyComponent) {
    

    Otherwise using @Output() is preferred to communicate from child to parent.

提交回复
热议问题