[英]Angular router - wrong url on redirection
I have problem with the Angular routing. 我在Angular路由方面遇到问题。 I am trying to do redirection from one page to another with parameter and as a result my browser tries to redirect to this link: ' http://localhost:4200/resumes/%5Bobject%20Object%5D/edit ' instead of this ' http://localhost:4200/resumes/21/edit '.
我正在尝试使用参数从一个页面重定向到另一页面,因此我的浏览器尝试将其重定向到此链接:' http:// localhost:4200 / resumes /%5Bobject%20Object%5D / edit '而不是此' http:// localhost:4200 / resumes / 21 / edit '。
app-routing.module.ts app-routing.module.ts
{ path: 'resumes/:id/edit', component: EditResumeComponent }
component.ts component.ts
import { Component, OnInit } from '@angular/core';
import { Resume } from 'src/app/models/Resume';
import { ResumeService } from 'src/app/services/resume.service';
import { Router } from '@angular/router';
import { UserService } from 'src/app/services/user.service';
import { AddUser } from 'src/app/models/AddUser';
@Component({
selector: 'app-supply-contact-information',
templateUrl: './supply-contact-information.component.html',
styleUrls: ['./supply-contact-information.component.css']
})
export class SupplyContactInformationComponent implements OnInit {
id: string;
resume: Resume;
constructor(
private resumeService: ResumeService,
private router: Router,
private userService: UserService) { }
ngOnInit() {
this.resume = this.resumeService.getResume();
}
onSubmit() {
this.resumeService.updateResume(this.resume);
const addUserRequest = new AddUser(this.resume.firstName, this.resume.lastName, this.resume.email, this.resume.phone);
this.userService.addUser(addUserRequest)
.subscribe(value => this.id = value.toString(),
() => {
// TODO: On error should be implemented here!
},
() => {
this.router.navigate([`/resumes/${this.id}/edit`]);
});
}
}
user.service.ts user.service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { AddUser } from '../models/AddUser';
import { environment } from '../../environments/environment';
import { catchError, tap } from 'rxjs/operators';
const httpOptions = {
headers: new HttpHeaders(
{
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
}),
};
@Injectable({
providedIn: 'root'
})
export class UserService {
private apiUrl = environment.apiUrl;
constructor(private http: HttpClient) { }
addUser(addUserQuery: AddUser): Observable<number> {
return this.http.post<number>(`${this.apiUrl}/user`, addUserQuery, httpOptions)
.pipe(
tap(() => this.log(`User with email: ${addUserQuery.email} created!`, false)),
catchError(this.handleError<any>('addUser'))
);
}
private handleError<T>(operation = 'operation', result?: T) {
return (error: any): Observable<T> => {
console.error(`${operation}: ${error}`);
return of(result as T);
};
}
// TODO: It should be implemented better later!
private log(message: string, showNotification: boolean) {
if (showNotification) {
console.log(message);
}
}
}
I tried to redirect like this, but getting same result: 我试图这样重定向,但得到相同的结果:
this.router.navigate(['resumes', id, 'edit']);
id
is a normal string property, not an object. id
是普通的字符串属性,而不是对象。 The AddUser
method returns number so I used the .toString()
method to make it a string. AddUser
方法返回数字,因此我使用.toString()
方法使其成为字符串。
since you have response as: {"resumeId":31}
由于您的回应为:
{"resumeId":31}
You should use (resumeId from value and use it for the navigation ): 您应该使用(来自值的resumeId并将其用于导航):
this.userService.addUser(addUserRequest)
.subscribe(value => this.id = value.resumeId.toString(),
() => {
// TODO: On error should be implemented here!
},
() => {
this.router.navigate([`/resumes/${this.id}/edit`]);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.