简体   繁体   English

Angular 路由混乱/奇怪的行为

[英]Angular routing confusion/strange behavior

I have a base route with three sibling routes.我有一条包含三个兄弟路线的基本路线。 The parent route routes to my task-list.component.ts which contains a navbar and router outlet.父路由路由到我的 task-list.component.ts,其中包含导航栏和路由器插座。

I would like to have a route param on the base route where I can add an optional token我想在基本路线上有一个路线参数,我可以在其中添加一个可选令牌

so when I navigate to http://localhost:4200 token should be undefined.所以当我导航到 http://localhost:4200 时,令牌应该是未定义的。

when I navigate to http://localhost:4200/123 token should be 123 in the activated route params当我导航到 http://localhost:4200/123 令牌在激活的路由参数中应该是 123

I have the below route config but i'm encountering confusing/strange behaviour.我有以下路由配置,但遇到了令人困惑/奇怪的行为。

When I navigate to http://localhost:4200 I get to my base taskList.component as expected.当我导航到 http://localhost:4200 时,我按预期到达了我的基本 taskList.component。

When I try navigate to http://localhost:4200/123 I get a 404 not found?当我尝试导航到 http://localhost:4200/123 时,我收到 404 not found? The expected bahaviour is that this should have navigated to taskList.component and added 123 to the activated route params...预期的行为是这应该导航到 taskList.component 并将 123 添加到激活的路由参数中......

even more strange when I click the deleted link in my navbar it navigates to the parent component app.component again only then I get "deleted" as the value in the activated route params...更奇怪的是,当我单击导航栏中已删除的链接时,它会再次导航到父组件 app.component,然后我才被“删除”为激活的路由参数中的值...

Even more strange: when I navigate to http://localhost:4200 using my browser it doesn't set deleted as token instead I get a 404 not found again...更奇怪的是:当我使用浏览器导航到 http://localhost:4200 时,它没有将 deleted 设置为令牌,而是我得到了 404 not found again ...

Any idea how I can achieve the above/what my issue might be?知道我如何实现上述目标/我的问题可能是什么吗?

my route module code:我的路线模块代码:

    import { NgModule } from '@angular/core';
    import { Routes, RouterModule } from '@angular/router';
    
    import { AppComponent } from './app.component';
    import { TaskListComponent } from './task/task-list/task-list.component';
    import { CompletedTasksComponent } from './task/completed-tasks/completed-tasks.component';
    import { DeletedTasksComponent } from './task/deleted-tasks/deleted-tasks.component';
    
    
const routes: Routes = [  
  { path: '', component: TaskListComponent, pathMatch: 'full' },
  { path: 'completed', component: CompletedTasksComponent },
  { path: 'deleted', component: DeletedTasksComponent },
  { path: ':token', component: TaskListComponent },  
  { path: ':token/completed', component: CompletedTasksComponent },
  { path: ':token/deleted', component: DeletedTasksComponent }
];
    
    @NgModule({
      imports: [RouterModule.forRoot(routes)],
      exports: [RouterModule]
    })
    export class AppRoutingModule { }

app.component.html:应用程序组件.html:

<nav mat-tab-nav-bar>
    <a mat-tab-link
    *ngFor="let link of links"
    [routerLink]="navigate(link)"
    (click)="activeLink = link"
    [active]="activeLink == link">{{link}}</a>
    

    </nav>


<router-outlet></router-outlet>

app.component.ts navigate method app.component.ts 导航方法

navigate(link) {
    switch(link) {
      case 'Task List':
        return `${this.token}`;
      case 'Completed Tasks':
        return `${this.token}/completed`;
      case 'Deleted Tasks':
        return `${this.token}/deleted`;
    }
  }

Old answer: You have some issues in your routes.旧答案:您的路线有一些问题。 you can fix it like:你可以像这样修复它:

    RouterModule.forRoot([
      { path: "", component: TaskListComponent, pathMatch: "full" },
      { path: "deleted", component: DeletedTasksComponent },
      { path: ":id", component: TaskListComponent },
      { path: ":id/completed", component: CompletedTasksComponent },
      { path: ":id/deleted", component: DeletedTasksComponent }
    ])

Run It On Stackblitz 在 Stackblitz 上运行

Update: based on your edit and comments, now in app navigation works but you get 404 when you refresh the page (even in development environment).更新:根据您的编辑和评论,现在在应用程序导航中可以正常工作,但刷新页面时会收到 404(即使在开发环境中)。 so try this: https://stackoverflow.com/a/35285068/4718434 .所以试试这个: https://stackoverflow.com/a/35285068/4718434 (Also on production, you should configure your server to return angular html file on every path.) (同样在生产环境中,您应该将服务器配置为在每个路径上返回 angular html 文件。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM