Angular:导航到请求的ID
我有一个表格添加一个名称,我想添加用户并使用他的ID导航到下一页。 例如:
,在页面上,我将带有URL的另一页导航:
http://localhost:4200/contact?id=5b07d5ec-627b-45f6-8361-becb9a742d40
我有一行的问题代码:
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}});
})
}})
}
在此功能中,有问题的代码行是:
const contact = res.find((a:any)=>{
return a.displayName === this.contactForm.value.displayName
因为我只检查是否有这样的名称,然后将其发送到他的URL,并带有ID 但是我不希望现有用户我想要一个新用户,我一旦添加了一个新ID,就会创建它 我只是没有找到所有功能的合适函数
new Edit :
路由:
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(): void {
this.route.queryParamMap.subscribe(
params =>{
const id = params.get('id');
if(id){
this.contactsService.GetContact(id)
.subscribe(
response =>{
this.contact = response;
}
)
}
}
)
}
I have a form for adding a name I want to add the user and navigate to the next page with his id.
for example:
And on the page I get navigation to another page with 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
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(): void {
this.route.queryParamMap.subscribe(
params =>{
const id = params.get('id');
if(id){
this.contactsService.GetContact(id)
.subscribe(
response =>{
this.contact = response;
}
)
}
}
)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试更改类似的应用程序routing.component.ts.ts:
在“父”组件中,按下按钮会产生这样的内容:
然后在contactcomponent.ts中,在ngoninit()的开头(){} {}的开始这样:
像这样的东西..看看动态路由。在没有演示的情况下,很难完美地编写工作解决方案。
如果您有时间,为我们制作Stackblitz演示
Try to change your app-routing.component.ts like this:
And in the "parent" component, where you press the button make something like this:
and then in the contactComponent.ts in the beginning of the NgOnInit() {} write this:
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