刷新浏览器后如何保持在相同的角度页面上

时间:2020-02-28 19:09:23

标签: angular angular6 angular7 angular2-routing angular8

我正在开发角度应用程序。每次刷新浏览器时,应用程序都会移至主页。有什么办法可以留在同一页面上吗?

更新:这是应用程序的路由。

export const secureRoutes: Routes = [
{path: '', component: DashboardComponent,  pathMatch: 'full'},
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard] },
{ path: 'user', component: UserComponent, canActivate: [AuthGuard] },
{ path: 'table', component: TableComponent, canActivate: [AuthGuard] },
{ path: 'typography', component: TypographyComponent, canActivate: [AuthGuard] },
{ path: 'icons', component: IconsComponent, canActivate: [AuthGuard] },
{ path: 'maps', component: MapsComponent, canActivate: [AuthGuard] },
{ path: 'notifications', component: NotificationsComponent, canActivate: [AuthGuard] },
{ path: 'upgrade', component: UpgradeComponent, canActivate: [AuthGuard] },
{ path: 'categories/new', component: AddEditCategoryComponent, canActivate: [AuthGuard] },
{ path: 'categories/view/:id', component: ViewCategoryComponent, canActivate: [AuthGuard] },
{ path: 'categories/edit/:id', component: AddEditCategoryComponent, canActivate: [AuthGuard] },
{ path: 'categories', component: CategoriesComponent, canActivate: [AuthGuard] },
{ path: 'categories/:category_id/view_product/:product_id',
        component: ViewProductComponent, canActivate: [AuthGuard] },
{ path: 'categories/:category_id/new_product', component: AddEditProductComponent, canActivate: [AuthGuard] },
{ path: 'categories/:category_id/edit_product/:product_id',
      component: AddEditProductComponent, canActivate: [AuthGuard] },
{ path: 'settings', component: SettingsComponent, canActivate: [AuthGuard] },
{ path: 'settings/edit', component: EditSettingsComponent, canActivate: [AuthGuard] },
{ path: 'users', component: UsersComponent, canActivate: [AuthGuard] },
{ path: 'users/view_client/:id', component: ClientComponent, canActivate: [AuthGuard] },
{ path: 'orders', component: OrdersComponent, canActivate: [AuthGuard], },
{ path: 'orders/:type/:id', component: OrderComponent, canActivate: [AuthGuard] },
{ path: 'adgroups', component: AdGroupsComponent, canActivate: [AuthGuard] },
{ path: 'adgroups/new', component: AddEditAdGroupComponent, canActivate: [AuthGuard] },
{ path: 'adgroups/view/:id', component: ViewAdGroupComponent, canActivate: [AuthGuard] },
{
    path: '**',
    component: PageNotFoundComponent
}

];

这也是上述路线中使用的AuthGuard。

    @Injectable()
    export class AuthGuard implements CanActivate {
      constructor(
        private router: Router,
        public afAuth: AngularFireAuth
      ) {
      }
      canActivate(): Observable<boolean> {
        return this.afAuth.authState.pipe(map((user) => {
          if (user) {
            return true;
          }
          this.router.navigate(['/login'])
          return false;
        }))
      }
    }

1 个答案:

答案 0 :(得分:1)

我解决了这个问题。路线还可以,但是我有一个带有语言切换功能的导航栏。正在调用此语言开关以检查语言,并在某些情况下导航到主页。

谢谢大家。