2
I have a component that shows an image and another component that should change the src of that image.
This is what I tried to do:
Profile.component.ts
alteraDadosPerfil(){
this.perfilService.alteraDadosPerfil(this.perfil,this.croppedImage)
.pipe(
take(1)
)
.subscribe((res) => {
if(this.croppedImage != null){ //Se foi alterado imagem então guarda a foto no localstorage e chama o método do serviço que altera a foto de perfil
localStorage.setItem('foto', this.croppedImage);
this.perfilService.alteraFotoPerfil(this.croppedImage);
}
},
(err: HttpErrorResponse) => {
})
}
Profile.service.ts
fotoPerfil: Subject<any> = new Subject<any>();
alteraFotoPerfil(base64Foto: string){
this.fotoPerfil.next(base64Foto);
}
Home.component.ts
avatarPerfil: string = null;
ngOnInit(){
this.avatarPerfil = localStorage.getItem('foto');
this.nomeUsuarioLogado = localStorage.getItem('nome');
this.emailUsuarioLogado = localStorage.getItem('emailUsuario');
this.perfilService.fotoPerfil.subscribe(res => { //Não está entrando aqui dentro
this.avatarPerfil = res
})
}
Where are you calling the altered methodFotoPerfil?
– renanvm
It’s in a function, I know it’s coming into this function because if I put a console.log('test') is printed at the right time, but for some reason subscribe doesn’t get the change
– veroneseComS
You are using the Subject?
– renanvm
Yes, no perfilService I declared: fotoPerfil: Subject<any> = new Subject<any>();
– veroneseComS
Are you not calling the unsubscribe somewhere before?
– renanvm
i do not use unsubscribe
– veroneseComS
Adds all your code involved in the problem.
– edson alves
Which module is the Service?
– renanvm
I changed the question by adding all the code
– veroneseComS
The service is imported and added in the providers of the two modules (Homecomponent which is the component that should receive the profile photo and the Profile component that should send the data to the homeComponent)
– veroneseComS
Remove the service from both and place only in the providers of the root module of your application
– renanvm
Did it work, any explanation for it? Add an answer explaining so I can give as the correct answer.
– veroneseComS
Yes, just a moment
– renanvm