【问题标题】:angular navigation router in observable can't work可观察的角度导航路由器无法工作
【发布时间】:2020-05-07 18:28:58
【问题描述】:

我的主文件 html 组件中有 un 按钮,当我调用 router.navigate['path'] 时,单击此按钮不起作用。

当我点击图片按钮时,我会在页面重新加载后重定向到正确的路径并返回 localhost:4200。

谁能帮帮我?

enter code here
<pre>
const routes: Routes = [
         { path: "", component: HomeComponent },
         { path: "home", component: HomeComponent },
         { path: "firstcomponent", component: FirstComponent },
         { path: "secondcomponent", component: SecondComponent },
         { path: "**", component: PagenotfoundComponent }
  ];
<ng-container *ngFor="let cat of category; let index=index">
  <div>
   <a href="#" (click)="detailCategorie(cat.id)" >
      <img [src]="cat.url"
      [alt]="cat.nom" [title]='cat.nom' />
   </a>
  </div>
</ng-container>
detailCategorie(id:number){

   this.service.getCategorie(id).subscribe(data=>{
    this.categorie = data;

 let nomCategorie : string = this.categorie.nom;


switch (nomCategorie) {
case 'first':
    this.router.navigate(["/firstcomponent"]);
    break;
case 'second':
    this.router.navigate(["/secondcomponent"]);
    break;
default:
    console.log("No categorie exists!");
    break;
}

 },err=>{
    console.log("Erreur retourne categorie choisie : ",err);
 });

}

【问题讨论】:

  • 您确定getCategorie 会在订阅时发出值吗?
  • 是的@Krujit getCategorie 发出一个订阅值

标签: angular


【解决方案1】:

我认为您应该从链接中删除 href="#"。我的解决方案是:

<a (click)="detailCategorie(cat.id)" >
  <img [src]="cat.url"
  [alt]="cat.nom" [title]='cat.nom' />

【讨论】:

    猜你喜欢
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 2016-10-24
    相关资源
    最近更新 更多