代码之家  ›  专栏  ›  技术社区  ›  Tanzeel

如何访问另一个组件中一个组件的变量[角度]

  •  0
  • Tanzeel  · 技术社区  · 6 年前

    我是新来的。我今天要做一件简单的事。我已经看过许多答案,但不能正确地执行它们。我想访问 filter-panel 在里面 filter-bar ,(我的两个自定义组件)。但两者都不是亲子关系。它们是独立的,虽然在同一个目录中。在这里我创建了一个 stackblitz . 这是我的密码:

    过滤器-面板.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);
        }
    }
    

    在做了更多的研究之后,我意识到使用它是毫无意义的 @ViewChild 在这种情况下。所以我试着做了个服务。我试过了 @Input() . 我也试过这个: How to use a variable from a component in another in Angular2

    2 回复  |  直到 6 年前
        1
  •  2
  •   Maniraj Murugan    6 年前

    一个新的服务叫做, filter-panel.service.ts setter() getter()

    import { Injectable } from '@angular/core';
    
    @Injectable()
    export class FilterPanelService {
    
      filterdata: any[];
      constructor() { }
    
       get data(): any{
        return this.filterdata;
      }
    
      set data(val: any){
        this.filterdata = val;
        console.log(this.filterdata);
      }
    
    }
    

    filter-panel.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() {}
    }
    

    而且在 filter-bar.component.ts

    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);
        }
    }
    

    Working Stackblitz..

        2
  •  1
  •   Yash Rami    6 年前

    如果两个组件都没有父子关系,并且您希望在它们之间传递数据,那么您可以实现 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';
    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);
         })
        }
    }
    
    
    推荐文章