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

2018-08-01  本文已影响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

上一篇下一篇

猜你喜欢

热点阅读