gpt4 book ai didi

angular - 如何在 Angular 2 中将方法从一个组件调用到另一个组件?

转载 作者:太空狗 更新时间:2023-10-29 19:36:21 34 4
gpt4 key购买 nike

我有两个不同的组件 - 一个组件具有用于列表用户的点击事件方法,我想继承(调用)详细信息页面上的点击事件方法(另一个组件))

怎么做,请帮帮我...

答案更新

感谢您的回答。

我已经通过使用扩展类和在构造函数中使用 super() 得到了解决方案。

头等舱

export class TeamsView {
constructor(private test:TestService){
}
...
}

二等舱

export class TeamsForm extends TeamsView {
constructor(private test:TestService, private router: Router){
super(test)
}
...
}

最佳答案

<强>1。从一个组件调用方法到另一个组件(如果它们不是父组件和子组件)只能使用服务。

    //FirstComponent

import {Component} from '@angular/core';
import {CommonService} from './common.service';

@Component({
selector: '<first-component></first-component>',
template: 'some html here',
providers: [CommonService]
})

export class FirstComponent {
constructor(private _service: CommonService) {
this._service.callMethodOfSecondComponent();
}

}


//CommonService

import {Subject} from 'rxjs/Subject';

@Injectable()
export class CommonService {
invokeEvent: Subject<any> = new Subject();

callMethodOfSecondComponent() {
this.invokeEvent.next(someValue)
}
}


//SecondComponent

import {Component, OnInit} from '@angular/core';
import {CommonService} from './common.service';

@Component({
selector: '<second-component></second-component>',
template: 'some html here',
providers: [CommonService]
})

export class SecondComponent {
constructor(private _service: CommonService) {
this._service.invokeEvent.subscribe(value => {
if(value === 'someVal'){
this.callMyMethod();
}
});
}

callMyMethod(){
//code block to run
}
}

2.子组件调用父组件的方法

    //Child Component

import {Component} from '@angular/core';
@Component({
selector: '<child-component></child-component>',
template: 'some html here',
})

export class ChildComponent {
@Output()
emitFunctionOfParent: EventEmitter<any> = new EventEmitter<any>();
constructor() {
}

someMethodOfChildComponent(){
this.emitFunctionOfParent.emit(someValue);
}

}

//ParentComponent

import {Component} from '@angular/core';
@Component({
selector: '<parent-component></parent-component>',
template: `some html
<child-component
(emitFunctionOfParent)="myMethod($event)">
</child-component>
some html
`,
})

export class ParentComponent {
constructor() {
}

myMethod(someValue){
// i am called
}

}

3.从父组件调用子组件的方法

//Child Component

import {Component} from '@angular/core';
@Component({
selector: '<child-component></child-component>',
template: 'some html here',
})

export class ChildComponent {

constructor() {
}

someMethodOfChildComponentToBeCalled(){
// i am called
}

}


//Parent Component

import {Component, OnInit} from '@angular/core';
import {ChildComponent} from './child.component';
@Component({
selector: '<parent-component></parent-component>',
template: `some html
<child-component>
</child-component>
some html
`
})

export class ParentComponent implements OnInit {
@ViewChild(ChildComponent) private _child:
ChildComponent;

ngOnInit() {
this._child.someMethodOfChildComponentToBeCalled();
}



}

除了这些之外,可能还有其他通信方式。 :)

关于angular - 如何在 Angular 2 中将方法从一个组件调用到另一个组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44908478/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com