美文网首页
angular同级组件间调用方法(非父子)

angular同级组件间调用方法(非父子)

作者: 一只飞 | 来源:发表于2018-08-01 17:36 被阅读0次

    Component 1:

    @component(
      selector:'com1'
    )
    export class com1{
      function1(){...}
    }
    

    Component 2:

    @component(
      selector:'com2'
    )
    export class com2{
      function2(){...
          // i want to call function 1 from com1 here
      }
    }
    

    1、

    <com1 #com1></com1><com2 (myEvent)="com1.function1()"></com2>
    
    <com1 #com1></com1><com2 [com1ref]="com1"></com2>
    

    定义模板引用变量,当做参数给com2,@input接收参数
    2、

    import { com1 } from './com1.component.ts';
    @component(
      selector:'com2',
      providers:[com1]
    )
    export class com2{
      constructor(private com1Obj: com1) {
        com1Obj. function1();
      }
    
      function2(){...
          // i want to call function 1 from com1 here
      }
    }
    

    参考:https://stackoverflow.com/questions/37587732/how-to-call-another-components-function-in-angular2

    相关文章

      网友评论

          本文标题:angular同级组件间调用方法(非父子)

          本文链接:https://www.haomeiwen.com/subject/hfvrvftx.html