Angular 12:解决后卫重定向到其当前组件,而不是导航到其目的地

发布于 2025-01-30 05:03:38 字数 1603 浏览 3 评论 0原文

当您登录时,应将其重定向到仪表板页面。当我们导航到仪表板时,我们将进行解决方案,以便在看到页面之前已经加载了数据。当我们直接进入仪表板的链接时,我没有遇到任何问题。数据已检索并显示在页面上。当我们从登录页面到仪表板页面导航时,Angular将返回登录页面,而无需在控制台中提供错误消息。检索数据的逻辑是在服务类中隔离的,并根据需要在组件中使用。

路由类< /strong>

(当您在 /登录页面上时,您想导航到 /仪表板 /模板Angular只会保持 /登录。直接进入 /dashboard /demplates时,数据将被加载,立即)

const routes: Routes = [
  {
    path: 'login',
    component: AuthComponent,
  },
  {
    path: 'dashboard',
    component: DashboardComponent,
    children: [
      { path: 'home', component: HomeComponent },
      {
        path: 'templates',
        component: TemplatesComponent,
        resolve: { templates: TemplateResolver },
      },
      { path: '**', redirectTo: 'home' },
    ],
  },
];

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

服务类

@Injectable({
  providedIn: 'root',
})
export class TemplateService {
  defaultUrl: string = [API URL];

  constructor(private http: HttpClient) {}

  getTemplates(): Observable<FullTemplateModel[]> {
    console.log('getTemplates');
    return this.http.get<FullTemplateModel[]>(this.defaultUrl);
  }
}

解决班级

@Injectable({
  providedIn: 'root',
})
export class TemplateResolver implements Resolve<FullTemplateModel[]> {
  constructor(private templateService: TemplateService) {}
  resolve(): Observable<FullTemplateModel[]> {
    console.log('resolving');
    return this.templateService.getTemplates()
  }
}

When you log in you should be redirected to the dashboard page. When we navigate to the dashboard we are going to do a resolve so that the data is already loaded even before we can see the page. When we go directlry to the link of the dashboard I am not experiencing any problems. The data is retrieved and is shown on the page. When we navigate from the login page to dashboard page, angular goes back to the login page without giving an error message in the console. The logic of retrieving data is isolated in a service class and used in the components as needed.

Routing class

(When you are on the /login page and you want to navigate to /dashboard/templates angular will just stay on /login. When you go directly to /dashboard/templates the data will be loaded immediately)

const routes: Routes = [
  {
    path: 'login',
    component: AuthComponent,
  },
  {
    path: 'dashboard',
    component: DashboardComponent,
    children: [
      { path: 'home', component: HomeComponent },
      {
        path: 'templates',
        component: TemplatesComponent,
        resolve: { templates: TemplateResolver },
      },
      { path: '**', redirectTo: 'home' },
    ],
  },
];

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

Service class

@Injectable({
  providedIn: 'root',
})
export class TemplateService {
  defaultUrl: string = [API URL];

  constructor(private http: HttpClient) {}

  getTemplates(): Observable<FullTemplateModel[]> {
    console.log('getTemplates');
    return this.http.get<FullTemplateModel[]>(this.defaultUrl);
  }
}

Resolve class

@Injectable({
  providedIn: 'root',
})
export class TemplateResolver implements Resolve<FullTemplateModel[]> {
  constructor(private templateService: TemplateService) {}
  resolve(): Observable<FullTemplateModel[]> {
    console.log('resolving');
    return this.templateService.getTemplates()
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文