Angular 如何访问另一个组件中一个组件的变量[角度]
我是新手。我今天在尝试一件简单的事情。我已经看过很多答案,但不能正确地实施它们。我想访问Angular 如何访问另一个组件中一个组件的变量[角度],angular,typescript,Angular,Typescript,我是新手。我今天在尝试一件简单的事情。我已经看过很多答案,但不能正确地实施它们。我想访问过滤器栏中的过滤器面板的一些变量(我的两个自定义组件)。但两者都不是亲子关系。它们是独立的,但在同一个目录中。在这里,我创建了一个。这是我的代码: 过滤器面板.component.ts import { Component, OnInit } from '@angular/core'; @Component({ ... }) export class FilterPanelComponent imp
过滤器栏
中的过滤器面板
的一些变量(我的两个自定义组件)。但两者都不是亲子关系。它们是独立的,但在同一个目录中。在这里,我创建了一个。这是我的代码:
过滤器面板.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
...
})
export class FilterPanelComponent implements OnInit {
public activeFilters: string[];
public text: string="hello world";
constructor() {
this.activeFilters = [
'Apple',
'Grapes',
'Bana'
];
}
ngOnInit() {}
import { Component, OnInit, ViewChild } from '@angular/core';
import { FilterPanelComponent } from './filter-panel/filter-panel.component';
@Component({
...
})
export class FilterBarComponent implements OnInit {
@ViewChild('FilterPanelComponent', {static : false}) filterPanel: FilterPanelComponent;
public values1: string[] = ['Philips'];
public values2: string[];
constructor() {
//this.values2=this.filterPanel.activeFilters;
}
ngOnInit() {
//console.log(this.values2);
}
}
import { Component, OnInit } from '@angular/core';
import { messageService } from '../MesssageService.ts'
@Component({
...
})
export class FilterPanelComponent implements OnInit {
public activeFilters: string[];
public text: string="hello world";
constructor(private messageService: MessageService) {
this.activeFilters = [
'Apple',
'Grapes',
'Bana'
];
}
ngOnInit() {}
this.messageService.sendMessage('data from filterPanelComponent'); // here you can also pass the object
}
import { Component, OnInit, ViewChild } from '@angular/core';
import { messageService } from '../MesssageService.ts'
@Component({
...
})
export class FilterBarComponent implements OnInit {
constructor(private messageService: MessageService) {
}
ngOnInit() {
this.messageService.getMessage().subscribe(response => {
// here we get the data from another component
console.log(response);
})
}
}
}
过滤条.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
...
})
export class FilterPanelComponent implements OnInit {
public activeFilters: string[];
public text: string="hello world";
constructor() {
this.activeFilters = [
'Apple',
'Grapes',
'Bana'
];
}
ngOnInit() {}
import { Component, OnInit, ViewChild } from '@angular/core';
import { FilterPanelComponent } from './filter-panel/filter-panel.component';
@Component({
...
})
export class FilterBarComponent implements OnInit {
@ViewChild('FilterPanelComponent', {static : false}) filterPanel: FilterPanelComponent;
public values1: string[] = ['Philips'];
public values2: string[];
constructor() {
//this.values2=this.filterPanel.activeFilters;
}
ngOnInit() {
//console.log(this.values2);
}
}
import { Component, OnInit } from '@angular/core';
import { messageService } from '../MesssageService.ts'
@Component({
...
})
export class FilterPanelComponent implements OnInit {
public activeFilters: string[];
public text: string="hello world";
constructor(private messageService: MessageService) {
this.activeFilters = [
'Apple',
'Grapes',
'Bana'
];
}
ngOnInit() {}
this.messageService.sendMessage('data from filterPanelComponent'); // here you can also pass the object
}
import { Component, OnInit, ViewChild } from '@angular/core';
import { messageService } from '../MesssageService.ts'
@Component({
...
})
export class FilterBarComponent implements OnInit {
constructor(private messageService: MessageService) {
}
ngOnInit() {
this.messageService.getMessage().subscribe(response => {
// here we get the data from another component
console.log(response);
})
}
}
在做了更多的研究之后,我意识到在这个场景中使用
@ViewChild
是毫无意义的。所以我试着做一个服务。我尝试了@Input()
。我也试过这个:。但我仍然无法实现解决方案。请更正。如果两个组件都没有父子关系,并且如果您希望在它们之间传递数据,那么您可以为相同的组件实现RxJS
主题。我希望它能帮助你
信息服务
import { Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';
@Injectable({ providedIn: 'root' })
export class MessageService {
private subject = new Subject<any>();
sendMessage(message: string) {
this.subject.next({ text: message });
}
clearMessages() {
this.subject.next();
}
getMessage(): Observable<any> {
return this.subject.asObservable();
}
}
过滤条.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
...
})
export class FilterPanelComponent implements OnInit {
public activeFilters: string[];
public text: string="hello world";
constructor() {
this.activeFilters = [
'Apple',
'Grapes',
'Bana'
];
}
ngOnInit() {}
import { Component, OnInit, ViewChild } from '@angular/core';
import { FilterPanelComponent } from './filter-panel/filter-panel.component';
@Component({
...
})
export class FilterBarComponent implements OnInit {
@ViewChild('FilterPanelComponent', {static : false}) filterPanel: FilterPanelComponent;
public values1: string[] = ['Philips'];
public values2: string[];
constructor() {
//this.values2=this.filterPanel.activeFilters;
}
ngOnInit() {
//console.log(this.values2);
}
}
import { Component, OnInit } from '@angular/core';
import { messageService } from '../MesssageService.ts'
@Component({
...
})
export class FilterPanelComponent implements OnInit {
public activeFilters: string[];
public text: string="hello world";
constructor(private messageService: MessageService) {
this.activeFilters = [
'Apple',
'Grapes',
'Bana'
];
}
ngOnInit() {}
this.messageService.sendMessage('data from filterPanelComponent'); // here you can also pass the object
}
import { Component, OnInit, ViewChild } from '@angular/core';
import { messageService } from '../MesssageService.ts'
@Component({
...
})
export class FilterBarComponent implements OnInit {
constructor(private messageService: MessageService) {
}
ngOnInit() {
this.messageService.getMessage().subscribe(response => {
// here we get the data from another component
console.log(response);
})
}
}
您可以创建一个服务来在组件之间共享数据 一个名为,
filter panel.service.ts
file的新服务,包含和方法
在过滤器面板.component.ts中设置如下值:
export class FilterPanelComponent implements OnInit {
public activeFilters: string[];
public text: string="hello world";
constructor(public filterPanelService:FilterPanelService) {
this.activeFilters = [
'Provider: CMS',
'Region: All',
'Provider Group:All',
'Provider: CMS',
'Region: All',
'Provider Group: All'
];
this.filterPanelService.data = this.activeFilters;
}
ngOnInit() {}
}
export class FilterBarComponent implements OnInit {
@ViewChild('FilterPanelComponent', {static : false}) filterPanel: FilterPanelComponent;
public values1: string[] = ['Philips'];
public values2: string[];
constructor(public filterPanelService: FilterPanelService) {
//this.values2=this.filterPanel.activeFilters;
}
ngOnInit() {
//console.log(this.values2);
console.log('value received ', this.filterPanelService.data);
}
}
然后在过滤器条.component.ts
中获得如下值:
export class FilterPanelComponent implements OnInit {
public activeFilters: string[];
public text: string="hello world";
constructor(public filterPanelService:FilterPanelService) {
this.activeFilters = [
'Provider: CMS',
'Region: All',
'Provider Group:All',
'Provider: CMS',
'Region: All',
'Provider Group: All'
];
this.filterPanelService.data = this.activeFilters;
}
ngOnInit() {}
}
export class FilterBarComponent implements OnInit {
@ViewChild('FilterPanelComponent', {static : false}) filterPanel: FilterPanelComponent;
public values1: string[] = ['Philips'];
public values2: string[];
constructor(public filterPanelService: FilterPanelService) {
//this.values2=this.filterPanel.activeFilters;
}
ngOnInit() {
//console.log(this.values2);
console.log('value received ', this.filterPanelService.data);
}
}
这回答了你的问题吗?查看最后一节“无关组件:与服务共享数据”@Tanzeel查看此链接。通过共享服务,我认为您不需要在组件之间进行任何通信。您只是使用字符串数组初始化两个组件的属性。为什么不将这些值保存在一个单独的常量文件中,并将其导入两个组件中。@Tanzeel,你能看看我下面的解决方案吗?我在这里将这个。activeFilters
数组从过滤器面板传递到过滤器条组件。我实现了这个解决方案,但仍然有一些不起作用。你能看看我刚刚创建的stackblitz:@Tanzeel当然!!错误显示:意外标记。需要一个构造函数、方法、访问器或属性。
现在有两个人帮助了我。:-)两者都在发挥作用。我现在接受谁呢?:-)嗨,亚什,你的答案是正确的。但我们认为另一个答案遵循最佳实践和首选的编码风格。非常感谢你的帮助。总有一天会再次打扰你:-)这就是我和@Yash想要做的。这个解决方案奏效了。让我把它放到生产环境中,并运行一些单元测试。我很快就回来。:-)@Tanzeel我不确定Yash的另一个解决方案。。但在这里,我已经为您提供了解决方案与工作stackblitz这正是你想要的。。此外,这与我在这里使用的getters
和setters
方法共享数据的解决方案不同。@Tanzeel,没有问题。。但我不太确定接受的解决方案是否是您需要的解决方案,因为当您单击文本过滤面板工作时,它会共享数据。我接受该解决方案是因为我想了解服务是如何创建和使用的。从那以后,我可以按自己的方式处理事情。:-)@坦泽尔,我不会为投票或接受而烦恼真的。。最后,我需要帮助人们解决这个问题,而不是其他问题。。