Angular2 中类似 React 的组件上下文

noi*_*euq 5 angular

我如何使用 Angular 做类似使用 React Context API 可以做的事情?例如,这样的事情:

<>
  <ThemeContext.Provider value="red">
    <Toolbar />
  </ThemeContext.Provider>
  <ThemeContext.Provider value="green">
    <Toolbar />
  </ThemeContext.Provider>
</>
Run Code Online (Sandbox Code Playgroud)

这是这个的工作示例:https://next.plnkr.co/edit/ESWRJapMh8k9wCe9

(我为 Angular 创建了类似的样板:https://next.plnkr.co/edit/OGnIpgnIsVC9wZXb。您只需提供一个工作叉作为答案即可。)

Kuz*_*bko 0

如果您可以添加第 3 方库,请检查此库。在其他情况下,您可以使用服务。

https://github.com/ng-turkey/ngx-context
Run Code Online (Sandbox Code Playgroud)