【问题标题】:How to reload the header component when the variable value changes via service?当变量值通过服务更改时如何重新加载标题组件?
【发布时间】:2019-06-25 20:31:30
【问题描述】:

我需要在标题中显示路由组件的标题。但是当我在我的应用程序中使用 ngOnInit 时,它会获得默认值。即使通过服务更改了变量值,它也不会改变。该怎么做?

Data.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable()

export class DataService {

  public myGlobalVar : string = "Chaitanya";

  constructor() { }

  setMyGV(val : string){
    this.myGlobalVar = val;
    console.log(this.myGlobalVar);
  }

  getMyGV(){
    return this.myGlobalVar;
  }
}

header.component.ts

    import { Component, OnInit } from '@angular/core';
    import { DataService } from 'src/app/data.service';

    @Component({
      selector: 'app-header',
      templateUrl: './header.component.html',
      styleUrls: ['./header.component.scss']
    })
    export class HeaderComponent implements OnInit {

      public title : string = '';

      constructor(private _emp : DataService) { }

      ngOnInit() {
        this.title = this._emp.getMyGV();    
      }
}

contact.component.ts

import { Component, OnInit } from '@angular/core';
import { DataService } from 'src/app/data.service';

@Component({
  selector: 'app-contact',
  templateUrl: './contact.component.html',
  styleUrls: ['./contact.component.scss']
})
export class ContactComponent implements OnInit {

  constructor(private _emp : DataService) { }

  ngOnInit() {
      this._emp.setMyGV('Contact');
      }
}

之前: 之后:

【问题讨论】:

  • 它没有改变,因为你在onInit中有代码
  • 标题组件已经正确加载,所以它如何检测标题的变化。在标头组件中将_emp 公开,并在html 中用作_emp.myGlobalVar。
  • 尝试在标题组件中使用 onChanges。我认为它会起作用。
  • @AndréPacheco : 如果是@Input 就可以了 :)
  • Onchanes 不工作

标签: angular


【解决方案1】:

我们可以直接在头部组件中使用服务变量,它可以工作。

{{ dataService.myGlobalVar }}

也不需要getter和setter,因为它是公共变量,我们可以直接使用它。

【讨论】:

    【解决方案2】:

    试试BehaviorSubject 而不仅仅是Subject

    service.ts

    import { Injectable } from '@angular/core';
    import { HttpClient } from '@angular/common/http';
    import { BehaviorSubject } from 'rxjs';
    
    @Injectable()
    
    export class DataService {
    
      public myGlobalVar$ = new BehaviorSubject<string>("Chaitanya");
    
      constructor() { }
    
      setMyGV(val : string){
        this.myGlobalVar.next(val);
      }
    
      getMyGV(){
        return this.myGlobalVar$.asObservable();
      }
    }
    

    在header.component中

    import { Component, OnInit } from '@angular/core';
    import { DataService } from 'src/app/data.service';
    import { Observable } from 'rxjs';
    
    @Component({
      selector: 'app-header',
      templateUrl: './header.component.html',
      styleUrls: ['./header.component.scss']
    })
    export class HeaderComponent implements OnInit {
    
      public title : Observable<string>;
    
      constructor(private _emp : DataService) { }
    
      ngOnInit() {
        this.title = this._emp.getMyGV();    
      }
    

    }

    在html中:

    {{ title | async}}

    【讨论】:

    • 为什么不是主题?
    • @firegloves:stackoverflow.com/questions/43348463/…。大多数时候,我们会因为使用Subject 而不是使用BehaviorSubject 而丢失数据,然后我们会花费更多时间来查找错误。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    • 2019-07-13
    • 2017-08-26
    • 1970-01-01
    • 2018-12-01
    • 2019-05-12
    相关资源
    最近更新 更多