【问题标题】:Angular not able to load another routesAngular 无法加载其他路线
【发布时间】:2021-12-31 08:31:06
【问题描述】:

尝试使用 Angular 11 创建 Web 应用程序时,我发现路由存在问题: 当我从初始路线导航时(

{ path: '', component: HomeComponent }

) 到另一条路线一切都按预期工作,但是当我尝试从另一条路线导航网站时,路由器会将我重定向到初始路线。 我无法从我的网络应用程序(“”除外)向其他人发送链接,因为路由器重定向到“”(主)路由。

例子:

const routes : Routes = [
  { path: '', component: HomeComponent },
  { path: 'product/:id', component: ProductComponent }
];

从 https://localhost:4200,当我访问 https://localhost:4200/product/14 工作正常, 但是当我尝试直接访问 https://localhost:4200/product/14 时,我将被重定向到 https://localhost:4200

如何配置我的 Angular 应用程序以接受任何初始路由?

【问题讨论】:

    标签: angular routes seo angular11


    【解决方案1】:

    尝试将其更改为 -

    const routes : Routes = [
      { path: 'home', 
        component: HomeComponent
      },
      { path: 'product/:id', component: ProductComponent },
      { path: '', 
        redirectTo: '/home',
        pathMatch: 'full'
      },
    ];
    

    【讨论】:

    • 在这种情况下,我被重定向到“/home”而不是“/”。
    【解决方案2】:

    尝试添加这个:

    const routes : Routes = [
      { path: '', 
        component: HomeComponent,
        pathMatch: 'full'
      },
      { path: 'product/:id', component: ProductComponent }
    ];
    

    【讨论】:

    • 我以前试过这个。仍然不是预期的行为:(
    • 那么我们应该需要更多关于所有路由的配置、路由模块、代码是否有子路由等信息。
    • 不,只有通用路由
    猜你喜欢
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 2019-09-25
    • 1970-01-01
    • 2018-12-11
    • 1970-01-01
    • 2021-10-04
    相关资源
    最近更新 更多