How to execute a function from another component that is NOT a sibling of the first component?

后端 未结 4 1592
长情又很酷
长情又很酷 2020-12-04 00:06

I\'m trying to execute a function from another component (These 2 components are NOT siblings). My guess is that I will need to use @Output and even

4条回答
  •  长情又很酷
    2020-12-04 00:48

    Inheritance the components.

    Let's say one component.

    @Component({
    ...
    })
    export class Component1 {
      constructor();
    
      public test1() {
        console.log("this is a test");
      }
    }
    

    you can inheritance the first component as a child an execute the test1 method

    @Component({
    ...
    })
    
    export Class Component2 extends Component1 {
      constructor();
    
      //Execute test1 method
      test1();
    }
    

    Also, remember that if you are using angular you need to export your components in the declarations and entryComponents. Also you need to import it in your new component.

    import { Component1 } from 'directory/component';
    

    /* Example */

    import { SplashScreen } from 'directory/splash-screen';

提交回复
热议问题