【问题标题】:Can I use hashing(#) and non hashing(#) routing both in angular project我可以在角度项目中使用散列(#)和非散列(#)路由吗
【发布时间】:2021-05-06 08:42:07
【问题描述】:

我想对一些路由使用散列(#) 和一些不使用散列(#) 的路由。我该如何实现这一点,例如,想要加载 attendance-report 带有散列和非散列的路径。这是我的 app.module.ts 文件

import { Routes, RouterModule } from "@angular/router";
import { AuthGaurdService } from "./auth/auth-gaurd/auth-gaurd.service";
import { PageNotFoundComponent } from "./dashboard/pages/page-not-found/page-not-found.component";
const routes: Routes = [
  { path: "login", redirectTo: "dashboard", pathMatch: "full" },
  {
    path: "attendance-report/:token",
    loadChildren: () =>
      import("./dashboard/pages/agent-attendance/agent-attendance.module").then(
        (m) => m.AgentAttendanceModule
      ),
  },
  {
    path: "",
    canActivate: [AuthGaurdService],
    children: [
      { path: "", redirectTo: "dashboard", pathMatch: "full" },
      {
        path: "",
        loadChildren: () =>
          import("./dashboard/dashboard-layout.module").then(
            (m) => m.AdminLayoutModule
          ),
      },
    ],
  },
  { path: "**", component: PageNotFoundComponent },
];

@NgModule({
  imports: [
    RouterModule.forRoot(routes, {
      useHash: true,
      relativeLinkResolution: "legacy",
    })
  ],
  exports: [RouterModule],
})
export class AppRoutingModule {}

【问题讨论】:

  • 您能否分享一下您将如何使用散列和非散列方法的上下文?

标签: javascript angular typescript frontend


【解决方案1】:

文档:Setting and handling query params and fragments

以下链接将查询参数和片段添加到生成的 URL:

<a [routerLink]="['/user/bob']" [queryParams]="{debug: true}" fragment="education">
  link to user component
</a>

默认情况下,该指令使用给定的查询参数构造新的 URL。该示例生成链接:/user/bob?debug=true#education

您可以通过在链接中指定queryParamsHandling 选项来指示指令以不同方式处理查询参数。允许的值为:

'merge':将给定的queryParams 合并到当前查询参数中。
'preserve':保留当前查询参数。 例如:

<a [routerLink]="['/user/bob']" [queryParams]="{debug: true}" queryParamsHandling="merge">
  link to user component
</a>

UrlCreationOptions#queryParamsHandling.

【讨论】:

猜你喜欢
  • 2021-07-14
  • 1970-01-01
  • 2012-02-21
  • 2012-10-25
  • 1970-01-01
  • 2015-12-14
  • 2011-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多