English 中文(简体)
Cant从路线参数中获取
原标题:Cant get id from route parameters

我有这种类型的<代码>和t;a>。

<a class="card_img"  [href]=" /Detail/  + food.id">

路线如下:

  {
    path: "Detail/:{Id}",
    component: DetailInfoComponent
  }

<>So>,问题: 我可以使用这种方法。

this.route.params.subscribe(params => {
     this.foodID = params[ Id ];
   });

http://www.ohchr.org。 <>?

问题回答
{
    path: "Detail/:Id",
    component: DetailInfoComponent
}

道路必须没有“{}”

你们可以借助天线或可观察的线索从路上获得红.。 我更喜欢观测站,因为URL有时会从方案上改变,如果模板/概览也更新,则会ice。

Your routing file must look like this (without the curly braces):

 {
    path: "Detail/:Id",
    component: DetailInfoComponent
 }

Since you want to wait for data bindings to be ready, you can access it in the ngOnInit lifecycle hook.


  // On top of your Class - For unsubscribing from observables automatically - Injection context
  private destroyRef = inject(DestroyRef);

  ngOnInit() {
    this.route.params.pipe(
      takeUntilDestroyed(this.destroyRef)
    ).subscribe(params => {
     console.log(params);
     this.foodID = params[ Id ];
   });

  }

Also added a console.log to make sure you re getting the value, and nothing else is going on with async operations.

为了让它使用一张头盔,你可以做(确保你用你预期的那套指数取代0):

const foodID = this.route.snapshot.url[0]!.path;

牢记每个平台上的档案名称和文件夹都对案件敏感。 个人只使用较低等级的申请人,但这可能是个人的偏好。

此外,使用[航道]而不是使用[红外线]来适当使用安热线航道。 例如。

<a class="card_img" [routerLink]="[ /Detail/ , food.id]">

这一轮船推进并播下了预期的导航线。 确保在进口时将路线摩杜勒包括在内。 [routerLink] exxpects or a string, or a range of path elements.





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签