从x.component.ts
我在呼叫getSomething()
y.service.ts
。我订阅了getSomething()
,因为它返回了一个可观察的值。事实是,在.subscribe()
之前,我有一个内部有4个元素的对象,其中一个是数字数组的数组(number [] []),而在{strong>之后 1}}同样的阵列现在是空的。
x.component.ts
.subscribe
在这个x.component中,我还尝试了一个this.y
.getSomething()
.pipe(
//make an observable to pass to child component
tap(data => (this.pautaFinal$ = of(data))),
// This object is just for development purpose
tap(data => (this.pautaFinal = data)),
// This is to change the number[][] object into a number[][][]
// I need it that way
tap(
data =>
(this.escalaPorcionada = this.transformEscala(
data.escala.escalaArr,
10
))
)
)
.subscribe();
// Makes an array of number arrays, into an array of arrays of chunkSize number arrays.
transformEscala(escala: number[][] = [], chunkSize: number): number[][][] {
let results: number[][][] = [];
while (escala.length) {
results.push(escala.splice(0, chunkSize));
}
return results;
}
,而不是第三个tap
,然后是`.subscribe(data => this.escalaPorcionada = this.transformEscala(data,10 )。
map(data => data.escala.escala)
这是函数调用即时在y.service.ts
getSomething(): Observable<Pauta> {
let admin: DatosAdmin;
let escala: Escala;
let rubrica: Rubrica;
let criterios: Criterios;
this.datosAdminAcumul$.subscribe(datos => (admin = datos));
this.escalaAcumul$.subscribe(datos => (escala = datos));
this.rubricaAcumul$.subscribe(datos => (rubrica = datos));
this.criteriosAcumul$.subscribe(datos => (criterios = datos));
let user = JSON.parse(localStorage.getItem("user"));
// Ultimos datos que agregar a la pauta.
let extras = {
usuarioModificacion: user.id
};
const pautaFinal: Pauta = {
datosAdmin: admin,
criterios: criterios,
rubrica: rubrica,
escala: escala,
extras: extras
};
return of(pautaFinal);
}
。它在同一服务中有一些可观测对象,它订阅了它们,获取了一些值,将它们分配给其他对象,然后将所有这些都放置在y.service.ts
对象中,这就是我返回作为可观察的。
我尝试过什么: 我已经检查铬调试器和前订阅数组存在,之后,它是空的。
这是的值pautaFinal
内可观察到的escalaArr
(从y.service)
该对象还有4个元素,并且只有this.escalaAcumul$
不变。
我不知道我在做什么错。我已经坚持了一段时间,感谢您的帮助。
答案 0 :(得分:1)
尝试在处理对象之前克隆Observable上交的对象。这样,您就可以打破JavaScript的“按引用”链。
this.y
.getSomething()
.pipe(
//make an observable to pass to child component
tap(data => (this.pautaFinal$ = of(data))),
// This object is just for development purpose
tap(data => (this.pautaFinal = data)),
// This is to change the number[][] object into a number[][][]
// I need it that way
tap(
data =>
(this.escalaPorcionada = this.transformEscala(
Object.assign({}, data.escala.escalaArr), // <-- clone here to avoid changing the original
10
))
)
)
.subscribe();
如果这不能解决问题,请检查是否有任何其他组件可以访问Observable的设置器。
编辑:
选项2:
transformEscala(escala: number[][] = [], chunkSize: number): number[][][] {
let results: number[][][] = [];
const clone = Object.assign({}, escala);
while (clone.length) {
results.push(clone.splice(0, chunkSize));
}
return results;
}