这是我的详细课程:
export class Details {
model = {
"name":"",
"city":"",
"pin" : ""
}
}
获取各种页面的实例:
import {Injectable} from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { Details } from './details';
@Injectable()
export class SharedDataService {
constructor(){}
object1 = new Details().model; //updates this data to object 2
object2 = new Details().model; //it is not empty, updated with object 1
sharedData = new BehaviorSubject( this.object1 );
sharedObserver = this.sharedData.asObservable();
sharedDataHello = new BehaviorSubject( this.object2 );
sharedObserverHello = this.sharedData.asObservable();
updateSharedData(object){
this.sharedData.next(object);
}
updateSharedDataHello(object){
this.sharedDataHello.next(object);
}
}
在object2
中-我正在寻找空对象。但是object1
的值已分配给object2
-如何防止这种情况?
Live demo-导航至hello
页并查看控制台。
答案 0 :(得分:0)
我更新了服务,如下所示:
import {Injectable} from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { Details } from './details';
@Injectable()
export class SharedDataService {
object1 = new Details().model;
object2 = new Details().model;
constructor(){
console.log( '1', this.object1);
console.log( '2', this.object2);
}
sharedData = new BehaviorSubject( this.object1 );
sharedObserver = this.sharedData.asObservable();
sharedDataHello = new BehaviorSubject( this.object2 );
sharedObserverHello = this.sharedDataHello.asObservable();
updateSharedData(object){
this.sharedData.next(object);
}
updateSharedDataHello(object){
this.sharedDataHello.next(object);
}
}