【发布时间】:2020-06-05 03:16:02
【问题描述】:
Within my anguular app , i ve this service :
@Injectable()
export class myService{
myBehaviouSubject= new BehaviorSubject("");
setData(){
this.myBehaviouSubject.next("123");
}
}
在我的 app.component 内部,我能够获取该值,但我想将其 readonly 保留或仅在服务本身内部可编辑,我想防止从组件推送任何数据 (.next('DATA'))
@Component({
})
export class AppComponent implements OnInit {
constructor(public myService : MyService) { }
getData(){
// GET VALUE
this.myService.myBehaviouSubject.value
}
unwantedMethodToSetValue(){
// SET VALUE -> i want to prevent this
this.myService.myBehaviouSubject.next("unwanted value")
}
}
建议?
【问题讨论】:
-
保护 .next() 的用例是什么?能否请您说明一下为什么这可能有用?
标签: javascript angular typescript rxjs behaviorsubject