angular - 用户以角度登录后显示侧边栏和仪表板组件
问题描述
我想在用户成功登录时显示仪表板和侧边栏组件。目前我的侧边栏组件和仪表板组件只有在我刷新页面后才会加载。
我尝试导航 this.router.navigateByUrl('/dashboard'); 用户成功登录 onClickSubmit 功能后
onClickSubmit() {
this.isValidFormSubmitted = false;
if (this.loginForm.invalid) {
this.formErrorSubmit = true;
return;
}
this.isValidFormSubmitted = true;
this.router.navigateByUrl('/dashboard');
// this.loginForm.reset();
}
html文件
<div id="main" class="page-wrapper" [ngClass]="{'toggled' : getSideBarState()}">
<app-sidebar></app-sidebar>
<main class="page-content">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<router-outlet></router-outlet>
</div>
</div>
</div>
<!-- <div class="overlay" (click)="toggleSidebar()" [ngClass]="{'show' : !getSideBarState()}"></div> -->
</main>
</div>
路由文件
const routes: Routes = [
{path: '', component:LoginComponent},
{path: 'dashboard', component:DashboardComponent},
{path: 'calendar', component:CalendarComponent},
];
侧边栏服务
toggled = false;
toggle() {
this.toggled = ! this.toggled;
}
getSidebarState() {
return this.toggled;
}
成功登录后应显示仪表板和侧边栏组件。
解决方案
使用角度路由作为以下示例。为此,您必须创建一个路由功能。
constructor(private customerService: CustomerService, private router: Router) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const redirectUrl = route['_routerState']['url'];
if (this.customerService.isLogged()) {
return true;
}
this.router.navigateByUrl(
this.router.createUrlTree(
['/login'], {
queryParams: {
redirectUrl
}
}
)
);
return false;
}
然后在你的 component.ts 文件中给出路由路径如下,
this.isValidFormSubmitted = true;
this.router.navigateByUrl('/dashboard');
进一步遵循这个例子。如果您有任何问题,请随时在下面发表评论。谢谢!
推荐阅读
- qemu - 如何从 qemu-system-m68k 中访问外部文件
- r - 如何使用 R dplyr 获取多列值的平均值
- python - Pandas .shift 函数产生一个“'float'对象没有属性'shift'”,'发生在索引0')
- c++ - 信号处理程序不适用于通过管道打开的进程
- php - Mysql 从不同的表中按名称选择
- reactjs - 我正在尝试获取我的产品消费者当前状态
- python - 关于在scrapy中重命名下载的图片
- forms - 如何删除或自定义选定的 Access 表单导航按钮周围的边框?
- javascript - 一个模块不能有多个默认导出
- firebase - 是否可以使用 firebase 手动更改用户电子邮件?