惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Y
Y Combinator Blog
D
DataBreaches.Net
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
云风的 BLOG
云风的 BLOG
Recorded Future
Recorded Future
I
InfoQ
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
宝玉的分享
宝玉的分享
Martin Fowler
Martin Fowler
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
A
About on SuperTechFans
人人都是产品经理
人人都是产品经理
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
C
Cybersecurity and Infrastructure Security Agency CISA
Know Your Adversary
Know Your Adversary
MongoDB | Blog
MongoDB | Blog
T
Tor Project blog
The Register - Security
The Register - Security
H
Help Net Security
Cisco Talos Blog
Cisco Talos Blog
P
Privacy & Cybersecurity Law Blog
NISL@THU
NISL@THU
P
Palo Alto Networks Blog
B
Blog RSS Feed
Latest news
Latest news
T
Threat Research - Cisco Blogs
The Hacker News
The Hacker News
C
Cisco Blogs
P
Privacy International News Feed
T
The Exploit Database - CXSecurity.com
V
Vulnerabilities – Threatpost
S
Schneier on Security
P
Proofpoint News Feed
Schneier on Security
Schneier on Security
www.infosecurity-magazine.com
www.infosecurity-magazine.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
AI
AI
Google Online Security Blog
Google Online Security Blog
H
Hacker News: Front Page
N
News and Events Feed by Topic
W
WeLiveSecurity

博客园 - Sameen

上下文工程学习笔记 什么是vibe ? 什么是 embedding ? vscode问题 document cookie JavaScript中的Date squash merge Element.scrollIntoView requestIdleCallback angular学习笔记 [转]React hooks useEffect中如何使用异步函数(即如何使用async/await) css斜条纹背景——linear-gradient 【转】过滤不可见字符 换电脑快捷同步vscode插件和配置 Git自动补全配置安装(Mac版本) Mac上看不到.git目录下的隐藏文件的解决方法 ts 问号点 ?. moment获取本月、上个月、近三个月时间段 iframe优缺点
angular中asObservable是什么的,为什么需要用?
Sameen · 2025-08-08 · via 博客园 - Sameen

Subject 本身既是观察者(Observer)又是可观察对象(Observable),这意味着它可以调用 next()error()complete() 等方法发送数据。但在实际开发中,我们通常希望 限制外部对 Subject 的直接操作,只允许外部订阅数据,而不能直接发送数据。

asObservable() 方法就解决了这个问题:

  • 它返回一个 只读的 Observable 视图,外部只能订阅这个 Observable 来接收数据
  • 外部无法调用 next() 等方法发送数据,保证了数据流的安全性和可控性
import { BehaviorSubject } from 'rxjs';

class DataService {
  // 内部使用 Subject 管理数据
  private dataSubject = new BehaviorSubject<string>('初始值');
  
  // 对外暴露只读的 Observable
  public data$ = this.dataSubject.asObservable();
  
  // 内部方法可以安全地发送数据
  updateData(newData: string) {
    this.dataSubject.next(newData);
  }
}

// 使用服务
const service = new DataService();

// 外部可以订阅数据
service.data$.subscribe(data => console.log('收到数据:', data));

// 外部无法直接调用 next(),以下代码会报错
// service.data$.next('非法修改'); // 错误:Property 'next' does not exist on type 'Observable<string>'

总结:

asObservable() 是一种封装技巧,通过隐藏 Subject 的写入能力(next() 等方法),只暴露读取能力(订阅),从而实现了数据流的单向性和可控性,这在 Angular 服务中共享数据时非常常用。