简体   繁体   English

如何在 Angular 中保持另一个组件通用的同时从另一个组件调用一个函数?

[英]How can I call a function from another component while keeping the other component generic in Angular?

I am trying get component1 to trigger a function in component2, but component2 needs to stay generic so it can be accessed by any component.我试图让 component1 触发 component2 中的函数,但 component2 需要保持通用,以便任何组件都可以访问它。 Without making it generic, my code looks like this:没有使其通用,我的代码如下所示:

component1:组件1:

@Output() component2function = new EventEmitter<any>();
...
this.component2function.emit({ param1, param2 });

component2 selector:组件 2 选择器:

<component1 (component2function)="component2function($event)"> </component1>

This should trigger component2function but now I want to trigger that same function without explicitly using the component1 selector, so that component2function can be triggered by components other than component1.这应该会触发component2function但现在我想在不显式使用component1选择器的情况下触发相同的函数,以便component2function可以由 component1 以外的组件触发。 How can I do this?我怎样才能做到这一点?

So you need some minimum agreement, that the implemented components has this method.因此,您需要一些最低限度的协议,即实现的组件具有此方法。 You can achieve this by defining an interface, which is inherited by component1 , ...:您可以通过定义一个接口来实现这一点,该接口由component1继承,...:

interface MyInterface {
    component2function: EventEmitter<any>;
}

(Soruce: How to interface Output() and Input() decorators? ) (来源: 如何接口 Output() 和 Input() 装饰器?

This way, don't have to know exactly what kind of component it is, but can make sure that has this method.这样,不必确切知道它是哪种组件,但可以确保具有此方法。

I would put the function into a shared service instead.我会将该功能放入共享服务中。 That way, all you need to do is provide the service via the constructor of each component.这样,您需要做的就是通过每个组件的构造函数提供服务。 Component 1, component 2, and component 3 then can all access this shared function easily.然后,组件 1、组件 2 和组件 3 都可以轻松访问此共享功能。

Service.ts服务.ts

@Injectible({
   providedIn: "root"
})
export class SharedService {
    sharedFunction() {
        let theSolution = ....;

        return theSolution;
    }
}

Then in the component.ts然后在 component.ts

@Component({
  selector: "component1",
  templateUrl: "./component1.component.html",
  styleUrls: ["./component1.component.css"]
})
export class Component1 implements OnInit {

    constructor(private sharedService: SharedService) {}

    ngOnInit() {
        this.genericFunction();
    }

    genericFunction() {
        this.sharedService.sharedFunction();
    }
}

From there you can just call either the service function right from the html or call the component function like above from the html.从那里你可以直接从 html 调用服务函数,或者从 html 调用上面的组件函数。 If you're managing data, the state will be more predictable in a shared service.如果您正在管理数据,则共享服务中的状态将更加可预测。

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

相关问题 如何从 angular 中的其他组件调用 function - How to call a function from other component in angular 如何从另一个组件调用函数 [Angular] - How to call a function from another component [Angular] 如何从 angular 中的 model 组件调用另一个组件 function? - How to call another component function from model component in angular? 如何从 Angular 中的另一个组件调用组件的 ngOnit function - How to call ngOnit function of a component from another component in Angular 如何从 Angular 材料对话框中调用另一个组件 function - How can I call another component function from Angular Material Dialog 如何从 angular 7 中的其他组件调用组件 - How to call a component from other component in angular 7 从通用组件 Angular 调用 function - Call function from generic component Angular Angular:引导程序 - 如何在从其他组件调用时在组件中使用全高? - Angular : Bootstrap - How can I use full height In a component while calling from other component? 如何在 AppComponent 以外的其他组件中使用从共享模块导入的 Angular 组件 - How can I use angular component imported from a shared module in another component other than AppComponent 如何从Angle 5中的其他组件更新组件数据 - How can i update component data from other component in angular 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM