Angular4を初めて使用しています。基本的な質問と貧しい私の英語のために申し訳ありません。私は自分の問題を説明するために最善を尽くします。絶対に破壊されないコンポーネントでAngular2/4脱退が観察される
私は現在、観測可能なサービスを使ってメッセージを共有するウェブサイトを開発しています。
しかし、問題が発生しました。 ここは私のexample file structureです。 working Plunker
。
「ユーザーコンポーネント」から「ヘッダーコンポーネント」受信メッセージがありました。
ヘッダコンポーネント:
import { Component, OnInit, OnDestroy} from '@angular/core';
import { Subscription } from 'rxjs/subscription';
import { SharedService } from "./sharedService";
@Component({
selector: 'shared-header',
template: `<div class="my-header">
<span>This is header </span>
<span class="right-align">Recieve Data:{{received}}</span>
</div>`,
})
export class HeaderComponent implements OnInit, OnDestroy{
private subscription: Subscription;
private received = "none";
constructor(private sharedService : SharedService) {}
ngOnInit(){
this.subscription = this.sharedService.shareAction$.subscribe(result => {
if(result){
this.received = result;
}
};
}
ngOnDestory(){
this.subscription.unsubscribe();
}
}
ユーザ・コンポーネント:
import { Component, OnInit, OnDestroy} from '@angular/core';
import { Subscription } from 'rxjs/subscription';
import { SharedService } from "./sharedService";
@Component({
selector: 'shared-header',
template: `<div class="my-header">
<span>This is header </span>
<span class="right-align">Recieve Data:{{received}}</span>
</div>`,
})
export class HeaderComponent implements OnInit, OnDestroy{
private subscription: Subscription;
private received = "none";
constructor(private sharedService : SharedService) {}
ngOnInit(){
this.subscription = this.sharedService.shareAction$.subscribe(result => {
if(result){
this.received = result;
}
};
}
ngOnDestory(){
this.subscription.unsubscribe();
}
}
と "ウィジェット成分"、 "製品コンポーネント" からメッセージを受信しました。
ウィジェットコンポーネント:
import { Component, OnInit, OnDestroy} from '@angular/core';
import { Subscription } from 'rxjs/subscription';
import { SharedService } from "./sharedService";
@Component({
selector: 'widget',
template: `<div>---Widget----</div>
<div>=={{productName}}==</div>`,
})
export class WidgetComponent implements OnInit, OnDestroy{
private productName = "none";
private subscription: Subscription;
constructor(private sharedService : SharedService) {}
ngOnInit(){
this.subscription = this.sharedService.shareAction$.subscribe(result => {
if(result){
this.productName = result;
}
};
}
ngOnDestory(){
this.subscription.unsubscribe();
}
}
製品のコンポーネント:
import { Component, OnInit, OnDestroy} from '@angular/core';
import { SharedService } from "./sharedService";
@Component({
template: `<h4>This is ProductPage</h4>
<widget></widget>`,
})
export class ProductComponent implements OnInit, OnDestroy{
constructor(private sharedService : SharedService) {}
ngOnInit(){
this.sharedService.publishData('NIKE');
}
}
私が観察サービスを処理するためのsharedServiceを作成します。 SharedService:「ユーザーページ」をクリックすると
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Subscription } from 'rxjs/subscription';
@Injectable()
export class SharedService {
private shareAction = new BehaviorSubject<any>(null);
shareAction$ = this.shareAction.asObservable()
sharedObject: object = {};
constructor() {}
publishData(data) {
console.log('publish------------------------------');
this.shareAction.next(data);
}
}
ので、ヘッダーの右側にある情報は「ユーザーページ」を表示します。 「商品ページ」をクリックすると、ウィジェットの下の情報に「NIKE」と表示されます。しかし同時に、ヘッダー情報も "NIKE"に変わります。私はそれが起こらないようにします。
コンポーネントが破棄されたときに私は退会しなければならないことを知っていますが、この場合、ヘッダーは決して破壊されません。だから、おそらく....私はそれを解除する必要があります "ユーザーコンポーネント"破棄し、再び "ユーザーコンポーネント" initを購読するが、私はそれを行う方法がわからない、またはこのアイデアが悪いです。
私はいくつかの指示を与えてください、任意の意見は助けになるでしょう。 ありがとうございました。
は、なぜあなたが言及したコンポーネントの関連する部分を追加していけませんか? –
ご意見ありがとうございます、私は私の質問を更新します。 – Ryan