简体   繁体   English

从组件 Angular 调用一个组件的函数

[英]Call function from one component from a component Angular

I'm a beginner in Angular.我是 Angular 的初学者。 I've been facing this issue with calling a method in a component from a component, which they are not related to.我一直面临着从组件调用组件中的方法的问题,它们与它们无关。 I followed a lot of tutorials on the internet but haven't found the solution.我在互联网上关注了很多教程,但没有找到解决方案。

So the detail is: There are 2 unrelated components.所以细节是:有2个不相关的组件。 The first component has a button and if I click that button, the string from the first component should be sent to a function in the second component and in that function It should display the string into the console.第一个组件有一个按钮,如果我单击该按钮,来自第一个组件的字符串应该被发送到第二个组件中的一个函数,并且在该函数中它应该将字符串显示到控制台中。 But the problem I'm facing here is that the function is called only once before I click and it just displays the value "111" which is the default value.但我在这里面临的问题是该函数在我单击之前只调用一次,它只显示值“111”,这是默认值。 Please help me请帮我

First component:第一个组件:

 export class FirstComponent implements OnInit{ constructor(location:Location, private renderer : Renderer2, private element : ElementRef, private router: Router, private httpClient: HttpClient, private servic: MainService) { } clickMe() { this.servic.sendMessage("001"); } }

Second component:第二部分:

 export class SecondComponent implements OnInit { clickEventSubs:Subscription; constructor(public servic: MainService, private spinner: NgxSpinnerService, private router: Router){} this.clickEventSubs = this.servic.receiveMessage().subscribe(message => { this.toggle(message); }) public toggle(state: string){ console.log(state); } }

Shared service:共享服务:

 @Injectable({ providedIn: 'root' }) export class MainService { private message = new BehaviorSubject<string>("111"); sendMessage(mess:string) { this.message.next(mess); } receiveMessage(): Observable<any> { return this.message.asObservable(); } }

When relationships between component is Child > Parent - Share date via @viewChild()当组件之间的关系是Child > Parent -通过 @viewChild() 共享日期

In First Component, Use @ViewChild() and pass second component name as an argument -在第一个组件中,使用 @ViewChild() 并将第二个组件名称作为参数传递 -

@ViewChild(SecondComponent) secondChildView!: SecondComponent;

Then call the second component's function in the first component's function -然后在第一个组件的函数中调用第二个组件的函数——

 import {ViewChild} from '@angular/core';

 export class FirstComponent implements OnInit{
 @ViewChild(SecondComponent) secondChildView!: SecondComponent;

 constructor() {}
 
   clickMe() {
     this.secondChildView.toggle('001'); 
  }
}

Whenever you call clickMe function it will call the toggle function of second component and you will get the value in toggle function from the first component.每当您调用 clickMe 函数时,它都会调用第二个组件的切换函数,您将从第一个组件获取切换函数中的值。

export class SecondComponent implements OnInit {

 constructor(){} 

 public toggle(state: string){
  console.log(state);
 }
}

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

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