Angular:导航到请求的ID

发布于 2025-02-13 05:16:23 字数 2312 浏览 1 评论 0原文

我有一个表格添加一个名称,我想添加用户并使用他的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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一花一树开 2025-02-20 05:16:23

尝试更改类似的应用程序routing.component.ts.ts:

  {path:'contact/:id',component:ContactComponent},

在“父”组件中,按下按钮会产生这样的内容:

   <button [routerLink]="['/contact', contact.id]">Add </button>

然后在contactcomponent.ts中,在ngoninit()的开头(){} {}的开始这样:

this.id = this.route.snapshot.paramMap.get('id');

像这样的东西..看看动态路由。在没有演示的情况下,很难完美地编写工作解决方案。
如果您有时间,为我们制作Stackblitz演示

Try to change your app-routing.component.ts like this:

  {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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文