Angular 更新服务中声明的变量时更新组件变量

Angular 更新服务中声明的变量时更新组件变量,angular,angular2-services,Angular,Angular2 Services,当我的服务中声明的变量发生更改时,我正在尝试更新组件中声明的变量。我正在使用这个主题。然而,什么也没有发生 应用程序模块.ts import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppComponent } from './app.component'; import { ShareDataService } from '.

当我的服务中声明的变量发生更改时,我正在尝试更新组件中声明的变量。我正在使用这个主题。然而,什么也没有发生

应用程序模块.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';


import { AppComponent } from './app.component';
import { ShareDataService } from './share-data.service';


@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [ShareDataService],
  bootstrap: [AppComponent]
})
export class AppModule { }
import { Component } from '@angular/core';
import { ShareDataService } from './share-data.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
  _subscription;
  constructor(private shareDataService:ShareDataService)
  {
    //this.title=this.shareDataService.title;
    this._subscription = shareDataService.titleChange.subscribe((value) => { 
      this.title = value; 
      //alert(this.title);
      console.log("COmponent::::::::::::::"+this.title);
    });
  }
}
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class ShareDataService {
  title:string="TITLE";
  titleChange: Subject<string> = new Subject<string>();
  constructor() {
    setTimeout(
      function()
      {

      this.title="BACDEF";

      //console.log(this.title);
      this.titleChange.next(this.title);
    }
    ,1000);
   }
}
应用程序组件.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';


import { AppComponent } from './app.component';
import { ShareDataService } from './share-data.service';


@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [ShareDataService],
  bootstrap: [AppComponent]
})
export class AppModule { }
import { Component } from '@angular/core';
import { ShareDataService } from './share-data.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
  _subscription;
  constructor(private shareDataService:ShareDataService)
  {
    //this.title=this.shareDataService.title;
    this._subscription = shareDataService.titleChange.subscribe((value) => { 
      this.title = value; 
      //alert(this.title);
      console.log("COmponent::::::::::::::"+this.title);
    });
  }
}
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class ShareDataService {
  title:string="TITLE";
  titleChange: Subject<string> = new Subject<string>();
  constructor() {
    setTimeout(
      function()
      {

      this.title="BACDEF";

      //console.log(this.title);
      this.titleChange.next(this.title);
    }
    ,1000);
   }
}
shareDataService.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';


import { AppComponent } from './app.component';
import { ShareDataService } from './share-data.service';


@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [ShareDataService],
  bootstrap: [AppComponent]
})
export class AppModule { }
import { Component } from '@angular/core';
import { ShareDataService } from './share-data.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
  _subscription;
  constructor(private shareDataService:ShareDataService)
  {
    //this.title=this.shareDataService.title;
    this._subscription = shareDataService.titleChange.subscribe((value) => { 
      this.title = value; 
      //alert(this.title);
      console.log("COmponent::::::::::::::"+this.title);
    });
  }
}
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class ShareDataService {
  title:string="TITLE";
  titleChange: Subject<string> = new Subject<string>();
  constructor() {
    setTimeout(
      function()
      {

      this.title="BACDEF";

      //console.log(this.title);
      this.titleChange.next(this.title);
    }
    ,1000);
   }
}
从'@angular/core'导入{Injectable};
从'rxjs/Subject'导入{Subject};
@可注射()
导出类ShareDataService{
title:string=“title”;
标题更改:主题=新主题();
构造函数(){
设置超时(
函数()
{
this.title=“BACDEF”;
//console.log(this.title);
this.titleChange.next(this.title);
}
,1000);
}
}
对于服务中定义的主题,它给出了一个错误,说明“无法读取未定义的属性'next'。实现这一点最合适的方法是什么?

使用箭头功能:

setTimeout(() => {
  this.title="BACDEF";
  this.titleChange.next(title);
}, 1000)
或绑定:

setTimeout(function() {
  this.title="BACDEF";
  this.titleChange.next(title);
}.bind(this), 1000)
为了摆脱那个错误。否则,setTimeout回调中的
为窗口对象

使用箭头函数:

setTimeout(() => {
  this.title="BACDEF";
  this.titleChange.next(title);
}, 1000)
或绑定:

setTimeout(function() {
  this.title="BACDEF";
  this.titleChange.next(title);
}.bind(this), 1000)
为了摆脱那个错误。否则,setTimeout的回调中的
是一个窗口对象

实现这一点最合适的方式是什么

您可以使用而不是
主题
,因为

  • 订阅时,BehaviorSubject返回最后一个值,而
    主题在
    onnext
    之前不会触发,因此使用
    BehaviorSubject
    您不必担心组件拥有最新数据,无论何时订阅

  • 如果要在不可观察代码(无订阅)中检索
    BehaviorSubject
    的最后一个值,可以始终使用
    getValue()
    方法

实现这一点最合适的方式是什么

您可以使用而不是
主题
,因为

  • 订阅时,BehaviorSubject返回最后一个值,而
    主题在
    onnext
    之前不会触发,因此使用
    BehaviorSubject
    您不必担心组件拥有最新数据,无论何时订阅

  • 如果要在不可观察代码(无订阅)中检索
    BehaviorSubject
    的最后一个值,可以始终使用
    getValue()
    方法


通知组件服务多个变量的变化的最佳方式是什么?使用Subject是最好的方法吗?Subject是一种自然的(我会说)方式来通知组件有关更改的信息。正如@cyberpirate92所写,你很可能需要一个行为主体。因为当您注入服务时,组件的构造函数需要该服务的实例。而且,由于服务没有生命周期挂钩,只有构造函数方法,所以当服务的主体已经收到next()时,您的组件将获得该实例。换句话说,构造函数在服务调用Subject上的next()后订阅。但如果您使用BehaviorSubject,它会记住最后一个值。通知组件一个服务的多个变量的变化的最佳方式是什么?使用Subject是最好的方法吗?Subject是一种自然的(我会说)方式来通知组件有关更改的信息。正如@cyberpirate92所写,你很可能需要一个行为主体。因为当您注入服务时,组件的构造函数需要该服务的实例。而且,由于服务没有生命周期挂钩,只有构造函数方法,所以当服务的主体已经收到next()时,您的组件将获得该实例。换句话说,构造函数在服务调用Subject上的next()后订阅。但如果使用BehaviorSubject,它会记住最后一个值。