简体   繁体   English

在另一个组件中调用已经存在的组件的方法

[英]call already existing component's method in another component

component 1 组成部分1

import { Component } from '@angular/core';
@Component({
    selector: 'app-component1',
    template:
    `
    <button (click)="submit()"></button>
    `
})
export class Component {

    constructor() { 
    }
    someMethod(): void {
    }
}

component 2 组成部分2

import { Component } from '@angular/core';
    @Component({
        selector: 'app-component2',
        template:
        `
        <p>text</p>
        `
    })
    export class Component2 {

        constructor() { 
        }
    }

i want to call the component 1 someMethod() from component 2 我想从组件2调用组件1 someMethod()

component 1 and component 2 haven't any parent/child relation. 组件1组件2没有任何父/子关系。

is there any way to get the instant of component 1 in component 2 有什么办法可以获取组件2组件1的瞬间

in java something like this 在java这样的东西

MyClass myClass = applicationContext.getBean("myClass");

is there any possible way like this in angular without BehaviorSubject and EventEmitter ? 没有BehaviorSubjectEventEmitter有什么可能的方式在角度EventEmitter

You can forward component A to the other component B via the parent template that contains them. 您可以通过包含它们的父模板将组件A转发到其他组件B。 There is no repository of component instances that you can query. 没有可查询的组件实例的存储库。 If you do not want to use the template to connect between the two, then you have to use a shared service instead. 如果您不想使用模板在两者之间进行连接,则必须使用共享服务。

app.component.html: app.component.html:

  <component-a #refA></component-a>
  <component-b [refA]="refA"></component-b>

  @Component({...})
  export class ComponentA {
      public someMethod() { }
  }

  @Component({...})
  export class ComponentB implement OnInit {
      @Input() refA: ComponentA;

      public ngOnInit() {
          if(this.refA) {
             this.refA.someMethod();
          }
      }
  }

The above has disadvantages. 上面有缺点。

  • you can trigger a this expression has changed after being checked error. 您可以在检查到错误触发此表达式已更改
  • you need to mark the view in ComponentA as dirty if you mutate the component state form outside. 如果您在外部更改组件状态表单,则需要将ComponentA的视图标记为脏。

I don't recommend the above, because mutating the internal state of another component from outside the view is an anti-pattern. 我不建议使用上述方法,因为从视图外部更改另一个组件的内部状态是一种反模式。 It is better to use a shared service and observables. 最好使用共享服务和可观察对象。

I would just create a service that has a handle on the other component to where you can pass the reference point of that component into: 我只是创建一个在其他组件上具有句柄的服务,您可以将该组件的参考点传递到该句柄中:

import { Component } from '@angular/core';
import { ComponentService } from 'filepathofyourcomponentservice';
@Component({
    selector: 'app-component2',
    template:
    `
    <p>text</p>
    `
})
export class Component2 {
    ViewChild('component) component: Component;

    constructor(private componentService: ComponentService) { 
        this.componentService.tapIntoComponent(this.component);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM