简体   繁体   English

Angular 服务将数据传递给组件

[英]Angular service pass data to component

I have search field in my top-bar component and I need to get input value of that search field in one of my components, but it doesn't pass the value.我的top-bar component中有搜索字段,我需要在我的一个组件中获取该搜索字段的输入值,但它没有传递该值。

Structure结构

  1. Search service搜索服务
  2. Top bar component顶栏组件
  3. Result component结果组件

Logic逻辑

  1. Top bar component receive input value then pass it to Search service Top bar component接收输入值,然后将其传递给Search service
  2. Result component receive input value from Search service and do it's process Result componentSearch service接收输入值并执行它的过程

What I have so far到目前为止我所拥有的

  1. Can get input value in Top bar component可以Top bar component中获取输入值
  2. Can't get any value in Result component无法Result component中获取任何值

Code代码

TopBar component

// HTML
<input nz-input nzBorderless placeholder="Search here..." (keydown.enter)="onInput($event)" style="background-color: #F3F6FC;" />

// SCRIPT

import { SearchServiceService } from 'src/app/services/search/search-service.service';
@Input() searchName: string;
constructor(
    private globalSearchService: SearchServiceService,
) {}

public onInput(event: any){
    // this pushes the input value into the service's Observable.
    this.globalSearchService.searchTerm.next(event.target.value);
}

Search service

export class SearchServiceService {
  public searchTerm: BehaviorSubject<string> = new BehaviorSubject<string>(null);
  constructor() { }
}

Result component

import { SearchServiceService } from 'src/app/services/search/search-service.service';

public searchTerm: string = "";

constructor(
  private globalSearchService: SearchServiceService
) { }

ngOnInit(): void {
  // this listens to the input value from the service and does something on change.
  this.globalSearchService.searchTerm.subscribe((newValue: string) => {
    console.log('search newValue :', newValue); // Nothing prints here!
    // this is where you would apply your existing filtering.
    this.searchTerm = newValue;
  });
}

Any idea?任何想法?

Update更新

Q: How did I register my search service?问:如何注册我的搜索服务?

A: A:

import { SearchServiceService } from 'src/app/services/search/search-service.service';

const MODULES = [CommonModule, RouterModule, AntdModule, TranslateModule, InfiniteScrollModule, FormsModule, ReactiveFormsModule]

@NgModule({
  imports: [...MODULES],
  declarations: [ACLComponent],
  exports: [...MODULES],
  providers: [SearchServiceService],
})
export class SharedModule {}

You're using the BehaviourSubject wrong here.您在这里使用了错误的BehaviourSubject Implement like so.像这样实现。 also please don't expose all your variables as public and remove constructors not being used也请不要将所有变量公开并删除未使用的构造函数

export class SearchServiceService {
  searchTerm: BehaviorSubject<string> = new BehaviorSubject<string>(null);
  $searchTerm = this.searchTerm.asObservable();
}

and then in your Result Component you subscribe to the Observable , not the BehaviourSubject然后在您的Result Component中订阅Observable ,而不是BehaviourSubject

import { SearchServiceService } from 'src/app/services/search/search-service.service';

searchTerm: string = "";

constructor(
  private globalSearchService: SearchServiceService
) { }

ngOnInit(): void {
  this.globalSearchService.$searchTerm.subscribe((newValue: string) => {
    this.searchTerm = newValue;
  });
}

Solved解决了

All I had to do was moving my service provider from shared module to app main module and rebuild (serve) my app.我所要做的就是将我的服务提供者从共享模块移动到应用程序主模块并重建(服务)我的应用程序。

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

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