old
import { Injectable } from '@angular/core'; import { BehaviorSubject } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class DataService { private dataSource = new BehaviorSubject(Object); public currentData = this.dataSource.asObservable(); constructor() {} changeData(data) { this.dataSource.next(data); } }
new :红色部分是重点
import { Injectable } from '@angular/core'; import { BehaviorSubject, Observable } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class DataService { private dataSource = new BehaviorSubject(Object); public currentData: Observable<Object> = this.dataSource.asObservable(); constructor() {} changeData(data) { this.dataSource.next(data); } }
问题:可观测属性的问题,
当我使用的是可观测的属性,但找不到它。要解决这个问题,我需要简单地添加缺少的可观察导入,并在当前数据变量中键入:然后就可以解决了