I have two angular components SignupComponent
and SignupSuccessComponent
. I want to pass data from the SignupComponent
to SignupSuccessComponent
, currently i have tried using a shared service with no luck.
@Component({
selector: 'app-signup',
templateUrl: './signup.component.html',
styleUrls: ['./signup.component.scss']
})
export class SignupComponent implements OnInit {
@ViewChild('signupForm') form: FormData;
constructor(public router: Router, private userService: UserService, private signupService: SignupService) { }
onSubmit() {
//user object is gotten from a network request
this.router.navigate(['/signup/success']);
this.signupService.setUser(user);
}
}
The SignupSuccessComponent
@Component({
selector: 'app-signup-success',
templateUrl: './signup-success.component.html',
styleUrls: ['./signup-success.component.scss']
})
export class SignupSuccessComponent implements OnInit {
public user: User;
constructor(public router: Router, private signupService: SignupService) {
}
ngOnInit() {
this.signupService.user$.subscribe(data => {
this.user = data;
console.log(this.user);
})
}
}
This is the shared service
@Injectable()
export class SignupService {
private user = new Subject<User>();
user$ = this.user.asObservable();
setUser(data: User) {
this.user.next(data);
}
}
It seem as though the shared service never gets any data in SignupSuccessComponent