ng2-rxjs subject的应用实例
ng2-rxjs subject的应用实例
import { Component } from '@angular/core'; import { Subject } from 'rxjs'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.less'] }) export class AppComponent { title = 'ng-rxjs'; private val1 = '111'; private val2: any; private val3 = '333'; val2_Subject$ = new Subject(); setVal2(str: any){ this.val2_Subject$.next(str); } ngOnInit() { this.val2_Subject$.subscribe(data => { this.val2 = data; }); console.log(this.val1); this.setVal2('222'); console.log(this.val2); console.log(this.val3); } }