角嵌套类别刷新路由器问题

问题描述 投票:0回答:1

我有一个看起来像这样的模块:

categories-routing.module.ts *

const routes: Routes = [
  { path: '', component: CategoriesComponent }, // this path does not reload
  { path: 'new', component: CategoryFormComponent, canActivate: [AuthGuard, EmailGuard] },
  { path: 'edit/:id', component: CategoryFormComponent, canActivate: [AuthGuard, EmailGuard] },
  {
    path: 'category', redirectTo: '',
    children: [
      {
        path: '**',
        component: CategoriesComponent
      }
    ]
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class CategoriesRoutingModule { }

categories.component.ts

@Component({
  selector: 'app-categories',
  templateUrl: './categories.component.html',
  styleUrls: ['./categories.component.scss']
})
export class CategoriesComponent {

  public categories!: Observable<any>;

  constructor(
    private nav: NavbarService,
    public cs: CategoryService,
    private route: ActivatedRoute,
    private router: Router
  ) {
    this.router.routeReuseStrategy.shouldReuseRoute = () => false;
  }

  ngOnInit() {
    this.categories = this.route.data.pipe(
      switchMap(() => {
        return this.cs.loadCategories();
      })
    );
  }

但是,在目录中浏览时效果很好,当我转到''路径时,它不会重新加载,而仅显示最后一个路径。我已经尝试以多种不同方式订阅激活的路线,以获得完全相同的结果。我也希望重新加载组件,而不是页面。

angular categories router reload angular9
1个回答
0
投票

因此事实证明,我的loadCategories()函数中存在一些错误的逻辑。至少经过两天的研究,我对路由器的了解比必要的多!

© www.soinside.com 2019 - 2024. All rights reserved.