我将http请求用作单独的服务。我也正在使用authguard作为服务。从authguard服务订阅后,我想将数据返回到登录组件。但是我无法将数据返回到组件。
http服务功能
createp(entity: any, ep:string): Observable<string> {
return this.http.post(ep, entity, {responseType: 'text'})
.pipe(
tap((reticapp: string) => this.log(`added entity w/ id=${reticapp}`)),
catchError(this.handleError<string>('create'))
);
}
身份验证服务功能
login(auth: Auth) {
sessionStorage.setItem('token', 'token');
const loginData: any = {
email: auth.email,
password: auth.password
};
const url = environment.apiEndpoint + '/pau/v1/ses';
this.authService.createp(loginData, url).subscribe(result => {
this.result = result;
});
}
从组件调用'onlogin()函数
onLogin(form: NgForm) {
if (form.invalid) {
return;
}
this.loginUser = {
email: form.value.email,
password: form.value.password
};
this.authService.login(this.loginUser);
}
我尝试在调用authservice之后添加添加订阅。但是添加页面时会刷新。
我想获取组件的返回数据如何获得?
答案 0 :(得分:2)
您应该像这样将subscribe移入组件ts文件中 服务:
login(auth: Auth) {
sessionStorage.setItem('token', 'token');
const loginData: any = {
email: auth.email,
password: auth.password
};
const url = environment.apiEndpoint + '/pau/v1/ses';
this.authService.createp(loginData, url);
}
组件
onLogin(form: NgForm) {
if (form.invalid) {
return;
}
this.loginUser = {
email: form.value.email,
password: form.value.password
};
this.authService.login(this.loginUser).subscribe(result => {
this.loginResult = result;
});
}