通过< router

编程入门 行业动态 更新时间:2024-10-25 18:23:46
本文介绍了通过< router-outlet>组件通信.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个具有更改的布尔值的根组件,我想在我的<router-outlet>中使用该组件来订阅该更改的布尔值.我了解我需要使用某种共享的双向服务.但是,共享服务文档对我来说意义不大. (我想我无法围绕宇航员的例子)这里,我们将不胜感激,这里有一些代码可以展示我的工作意图.

I have a root component that has a changing boolean and I want to subscribe to that changing boolean, with a component within my <router-outlet>. I understand I need to use a shared bidirectional service of some sort. However, the docs for shared services just arent making a whole lot of sense to me. (I guess I cant wrap my head around the astronaut example) here, any help would be greatly appreciated, here is a little bit of code to show what I am trying to do.

根组件

@Component({ selector: 'my-app', template: `<nav [state]="boolshow" (show)="changeValue($event)" ></nav> <article><router-outlet></router-outlet></article> <-----component in router here <footer></footer> <h3>toggle state: {{boolshow}}</h3>`, styleUrls: ['./Css/app.css'], }) export class AppComponent { boolshow: boolean; <-----boolean that needs to be read }

推荐答案

Angular2文档:

  • 使用可观察的方式创建服务

  • Create a Service with an Observable

在两个组件中注入相同的服务

Inject the same Service in both components

从一个组件中将数据更新到服务

From one component you update the data to the Service

从其他组件中读取服务中的数据

From the other component you read the data from the Service

例如.

服务:

@Injectable() export class DataService { private dataObs$ = new Subject(); getData() { return this.dataObs$; } updateData(data: boolean) { this.dataObs$.next(data); } }

组件:

@Component({ selector: 'my-app', template: `<div (click)="updateData(false)">Click t oupdate Data FALSE</div> <div (click)="updateData(true)">Click to update Data TRUE</div> <child></child> ` }) export class AppComponent { constructor(private dataService: DataService) {} updateData(value: boolean) { this.dataService.updateData(value); } } @Component({ selector: 'child', template: `<div><p>data from Service HERE:</p><p style="color:red"> {{myData}}</p></div>` }) export class ChildComponent { myData: boolean; constructor(private dataService: DataService) {} ngOnInit() { this.dataService.getData().subscribe(data => { this.myData = data; }) } }

确保在组件(Singleton)中注入了相同的服务:

@NgModule({ imports: [ BrowserModule, HttpModule ], declarations: [ AppComponent, ChildComponent ], providers: [ DataService ], bootstrap: [ AppComponent ] })

可以在 HERE 中找到完整的示例: plnkr.co/edit/nJVC36y5TIGoXEfTkIp9?p=preview

A full working example can be found HERE : plnkr.co/edit/nJVC36y5TIGoXEfTkIp9?p=preview

PS:这就是通过Service在Angular2中进行通信的方式,无论您的组件是否位于通过路由器的路由器出口内,它都可以在任何地方使用.

PS: This is how the communication via Service works in Angular2, it doesn't matter if your component is inside a router-outlet via a router, it works everywhere.

更多推荐

通过&lt; router

本文发布于:2023-08-03 08:05:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1285478.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:amp   lt   router

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!