gpt4 book ai didi

javascript - Angular如何在组件中显示post请求的结果

转载 作者:行者123 更新时间:2023-11-30 19:44:16 25 4
gpt4 key购买 nike

在我的 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"

最佳答案

对于 Angular 7.2.0 及更高版本:

您可以使用 NavigationExtras 来制作它内置于 Angular 路由器中。为此,您只需添加两件事。

第一步:

更新 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;

关于javascript - Angular如何在组件中显示post请求的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55083161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com