[英]Angular: Navigation to the requested Id
I have a form for adding a name I want to add the user and navigate to the next page with his id.我有一个用于添加名称的表单,我想添加用户并使用他的 ID 导航到下一页。 for example:
例如:
And on the page I get navigation to another page with Url:在页面上,我可以使用 Url 导航到另一个页面:
http://localhost:4200/contact?id=5b07d5ec-627b-45f6-8361-becb9a742d40
I have a problem with one line of code:我对一行代码有疑问:
addContact(){
this.contactsService.GetAllContacts()
.subscribe(res=> {
const contact = res.find((a:any)=>{
return a.displayName === this.contactForm.value.displayName
});
if(contact){
this.contactsService.AddContact(this.contactForm.value)
.subscribe(() => {
alert("Signup Succssfull");
this.contactForm.reset();
this.router.navigate(['contact'] , { queryParams: {id: contact?.id}});
})
}})
}
In this function the problematic line of code is:在这个函数中,有问题的代码行是:
const contact = res.find((a:any)=>{
return a.displayName === this.contactForm.value.displayName
Because I only check if I have such a Name then send it to his URL with the ID But I do not want an existing user I want a new user I create it as soon as I do ADD get a new ID I just did not find a suitable function of all the functions因为我只检查我是否有这样的名称,然后用 ID 将其发送到他的 URL 但我不想要现有用户我想要一个新用户我一做就创建它 ADD 得到一个我只是没有找到的新 ID所有功能的合适功能
New Edit :新编辑:
Routing:路由:
export const routes : Routes = [
{path:'',redirectTo:'login',pathMatch:'full'},
{path:'login' , component:LoginComponent},
{path:'signup', component:SignupComponent},
{path:'home', component:HomeComponent},
{path:'help', component:HelpComponent},
{path:'game', component:GameComponent},
{path:'app',component:AppComponent},
{path:'default',component:DefaultLayoutComponent},
{path:'contact',component:ContactComponent},
{path:'details',component:DetailsComponent},
{path:'test',component:TestComponent},
{path:'addContact',component:AddContactComponent},
{path:'**' , component:NotFoundComponent},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
NgOnInit: NgOnInit:
ngOnInit(): void {
this.route.queryParamMap.subscribe(
params =>{
const id = params.get('id');
if(id){
this.contactsService.GetContact(id)
.subscribe(
response =>{
this.contact = response;
}
)
}
}
)
}
Try to change your app-routing.component.ts like this:尝试像这样更改您的 app-routing.component.ts:
{path:'contact/:id',component:ContactComponent},
And in the "parent" component, where you press the button make something like this:在“父”组件中,您按下按钮的位置如下所示:
<button [routerLink]="['/contact', contact.id]">Add </button>
and then in the contactComponent.ts in the beginning of the NgOnInit() {} write this:然后在 NgOnInit() {} 开头的 contactComponent.ts 中写下:
this.id = this.route.snapshot.paramMap.get('id');
Something like this.. Look at the dynamic routing.像这样的东西..看看动态路由。 Its hard to perfectly write working solution without demo.
没有演示很难完美地编写工作解决方案。 If you have time, make StackBlitz demo for us
如果您有时间,请为我们制作 StackBlitz 演示
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.