简体   繁体   English

如何从另一个组件调用函数 [Angular]

[英]How to call a function from another component [Angular]

I'm new to Angular.我是 Angular 的新手。 This question is very much similar to mine but doesn't answers my question. 这个问题与我的非常相似,但没有回答我的问题。 In my case, the two components are stand-alone, that means they ain't parent-child to each other.就我而言,这两个组件是独立的,这意味着它们不是彼此的父子。 They're separate and at same directory level.它们是分开的并且处于相同的目录级别。 That's why my problem is different from the rest.这就是为什么我的问题与其他问题不同的原因。 I tried using:我尝试使用:

  1. @ViewChild(); @ViewChild();
  2. @Output() ....EventEmitter() @Output() ....EventEmitter()

But I'm still getting an error in navbar.component.ts :但我仍然在navbar.component.ts遇到错误:

ERROR TypeError: "this.articles is undefined"错误类型错误:“this.articles 未定义”

My first component is NavBar and my second component is Articles我的第一个组件是NavBar ,我的第二个组件是Articles

So the scenarioa is, pressed() method of NavBar have to call callSearch(arg) method of Articles .所以scenarioa是, pressed()方法NavBar必须调用callSearch(arg)的方法Articles

Here is my navbar.component.ts这是我的navbar.component.ts

import { ..., ViewChild } from '@angular/core';
import { ArticlesComponent } from '../articles/articles.component';

@Component({
  ...
})
export class NavbarComponent implements OnInit {

  text='something';

  @ViewChild(ArticlesComponent, {static: false}) articles: ArticlesComponent;

  constructor() { }

  /*  This method is called on click event from HTML page*/
  pressed(text: string) {
    this.articles.callSearch(text);
  }

  ngOnInit() {
  }
}

navbar.component.html导航栏.component.html

<li class="nav-item">
  <button class="btn" (click)="pressed(text)">Search</button>
</li>

And here is the component whose method I want to call from NavBar.这是我想从 NavBar 调用其方法的组件。

articles.component.ts文章.component.ts

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

@Component({
  ...
})
export class ArticlesComponent implements OnInit {

  articles = [];
  filteredArticles=[];

  constructor(...) { }

  ngOnInit() {
    ...
  }

  /*  this is called from navbar typescript and it will call SearchFunction below*/
  callSearch(text: string) {
    this.SearchFunction(text);
  }

  SearchFunction(text: string) {
    this.filteredArticles=(this.articles.filter(e => {
      /* some logic*/
    }));
  }
}

articles.component.html文章.component.html

<div *ngFor="let article of filteredArticles; let i = index;">
  <div class="card text-center">
    <div class="card-body">
      <!-- card design related html -->
    </div>
  </div>
</div>

Please correct me.请纠正我。

PS: Here is the stackblitz . PS:这是stackblitz

To communicate two components that are not related to each other, you can use a service.要通信两个彼此不相关的组件,您可以使用服务。

    @Injectable({
    providedIn: 'root',
})
export class YourService {

    private yourVariable: Subject<any> = new Subject<any>();


    public listenYourVariable() {
        return this.yourVariable.asObservable();

    }


    private yourVariableObserver(value : type) {
        this.yourVariable.next(value);
    }

You import in yours components where you want use it this service.您可以在您想要使用此服务的地方导入您的组件。

import{ YourService } from ...

In component you want write the data call:在您要编写数据调用的组件中:

this.yourService.yourVariableObserver(yourData);

while where you want read the data:而你想要读取数据的地方:

 this.yourService.listenYourVariable().subscribe(
    variable => {
      this.data = variable;
      this.callyourFunction();
    }

)

Obviously you can simple call the function without pass any value.显然,您可以简单地调用该函数而无需传递任何值。

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

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