在我的Angular应用程序中,我试图在另一个组件(即确认页面)中显示发布请求的结果。我想知道什么是最好的方法。在我的组件中,代码执行一个发布请求,该请求如下所示发布到服务器:
import { Component, OnInit } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { FormGroup, FormControl } from '@angular/forms';
import { Validators } from '@angular/forms';
import { FormBuilder } from '@angular/forms';
import { Request } from '../../models/request.model'
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
import { AppComponent } from '../../../app.component';
import { nowService } from '../../services/now.service';
import { HttpClient, HttpEventType, HttpHeaders, HttpRequest, HttpResponse } from '@angular/common/http';
@Component({
selector: 'app-service-request',
templateUrl: './service-request.component.html',
styleUrls: ['./service-request.component.scss']
})
export class ServiceRequestComponent implements OnInit {
public loading = false;
private customer_id = this.appComponent.customer_id;
serviceForm;
u_destination_country = [
{ value: 'Choose an option' },
{ value: 'United Kingdom', },
{ value: 'United States of America', },
{ value: 'Russia', },
{ value: 'Moscow', },
{ value: 'Africa', },
];
users = [
{ id: 'Select an option', },
{ id: '1', },
{ id: '2', },
{ id: '3', },
];
devices = [
{ id: 'Select an option', },
{ id: '1', },
{ id: '2', },
{ id: '3', },
];
constructor(private service: nowService,
private appComponent: AppComponent,
private router: Router,
private http: HttpClient
) {
}
ngOnInit() {
this.serviceForm = new FormGroup({
customer_id: new FormControl(this.customer_id),
si_id: new FormControl('', Validators.required),
u_destination_country: new FormControl(this.u_destination_country[0], Validators.required),
u_short_description: new FormControl('', Validators.compose([
Validators.required,
Validators.minLength(5),
Validators.maxLength(80)
])),
u_message_description: new FormControl(''),
});
}
onSubmit() {
this.http.post("/api/inc",
this.serviceForm.value,
{
headers: new HttpHeaders().set("Content-Type", "application/json")
}
).subscribe((response: any) => {
console.log(response);//On success response
this.router.navigate(['/inc/confirmation']);
}, (errorResponse: any) => {
console.log(errorResponse); //On unsuccessful response
});
if(this.serviceForm.invalid) {
this.serviceForm.setErrors({ ...this.serviceForm.errors, 'required': true });
return;
}
}
}
所以我要做的是显示display_value
的值,就像这样:
result: Array(1)
0:
display_name: "number"
display_value: "INC001"
status: "inserted"
table: "incident"
答案 0 :(得分:1)
对于Angular 7.2.0及更高版本:
您可以使用Angular Router内置的NavigationExtras来实现。为此,您只需添加两件事。
第一步:
将this.route.navigate(['/inc/confirmation'])
更新为此
this.router.navigate(['/inc/confirmation'],{state: {response}});
第二步:
在第二个组件中,您可以通过将其添加到代码中来访问state
const state = this.router.getCurrentNavigation().extras.state;
this.response = state.response;
然后您可以显示它。
对于Angular 7及更低版本:
第一步:
将this.route.navigate(['/inc/confirmation'])
更新为此
this.router.navigate(['/inc/confirmation'],{queryParams: {value: response.result[0].display_value}});
第二步:
在第二个组件中,您可以通过将其添加到代码中来访问state
constructor(private route: ActivatedRoute){}
...
this.value = this.route.snapshot.queryParamMap.get('value');
然后您可以显示它。
当您不想使用URL参数时:
您可以使用一个属性创建服务
@Injectable({
providedIn: 'root'
})
export class SharedDataService {
public sharedData;
constructor() {
}
}
然后,您可以在重定向到其他路由之前设置sharedData。
constructor(private sharedService: SharedDataService){}
...
this.sharedService.sharedData = response;
this.route.navigate(['/inc/confirmation']);
您可以在第二个组件中获取该数据。
constructor(private sharedService: SharedDataService){}
...
this.response = this.sharedService.sharedData;