【发布时间】:2017-05-02 12:21:06
【问题描述】:
我目前陷入这样一种情况,即我有 3 个不同的组件,其中一个在两个组件之间共享,如果我从两个组件中的任何一个更新共享组件,我希望它们同时更新它们是有办法吗?
ist-component
import {Component,bind,CORE_DIRECTIVES,OnInit} from 'angular2/core';
import {MainComponent} from 'src/MainComponent';
@Component({
selector: 'my-app',
directives:[MainComponent],
template: `<h1>AppComponent {{onMain}}</h1>
<div *ngIf="onMain == false">
Hello
<br> __________________________________<br>
</div>
<app-share></app-share>
})
export class FirstComponent implements OnInit {}
第二个组件
import {Component,bind,CORE_DIRECTIVES} from 'angular2/core';
import {SharedService} from 'src/shared.service';
@Component({
selector: 'sec-app',
template: `<app-share></app-share>
<button (click)='changeCount()'></button>
`
})
export class SecondComponent {
constructor(ss: SharedService) {
this.ss = ss;
}
changeCount() {
//want to change the count of shared component and also want a sync in first componen
}
}
共享组件
import {Component,bind,CORE_DIRECTIVES} from 'angular2/core';
@Component({
selector: 'app-share',
template: `{{count}}`
})
export class ShareComponent {
count:any;
}
现在两个组件都加载在不同的路径上,即带有两个选项卡的浏览器,一个选项卡具有第一个组件,第二个选项卡具有第二个组件,所以现在如果我更新第二个组件,我想要两个组件之间的同步必须反思第一个
【问题讨论】:
-
共享是什么意思?它们之间的父子关系是什么?
-
分享你目前尝试过的代码?
-
我用你现在可以看到的代码更新了我的问题
-
我猜问题必须是
How do you sync data between two browser tabs in angular2?
标签: angular angular2-components