简体   繁体   English

后退按钮返回登录页面,而不是角度4中的主页

[英]back button going back to login page instead of home page in angular 4

I am facing weird problem with back button press on browser, I have login page when i click on login page it goes to home page.. 我在浏览器中按下后退按钮时遇到了奇怪的问题,当我单击登录页面时,我进入了登录页面。

In the home page i have menu options like reports and sales when i click on reports page it goes to reports page. 在主页上,当我单击报告页面时,我具有菜单选项,例如报告和销售,然后转到报告页面。

My problem is when i click on browser back button on reports page it goes to login page instead of home page 我的问题是,当我单击报告页面上的浏览器后退按钮时,它会转到登录页面而不是主页

URL for Home page : /localhost:4200/home 主页的URL:/ localhost:4200 / home

URL for reports Page : /localhost:4200/home/monthlyreport/my 报告的URL页面:/ localhost:4200 / home / monthlyreport / my

and i have written code for browser back button below 而且我已经为下面的浏览器后退按钮编写了代码

import { PlatformLocation, Location } from '@angular/common'

constructor(private location: PlatformLocation, private loc: Location) {

      location.onPopState(() => {
       //window.history.back();
        this.loc.back(); // tried with this one but no result
     });
}

Could any one please help on this how to back to previous page using angular 4 Many thanks in advance.. 任何人都可以帮忙,如何使用angular 4返回上一页。非常感谢。

updated login action method 更新的登录操作方法

   const routes: Routes = [
  {
    path: "",
    redirectTo: "login",
    pathMatch: "full"
  },
  {
    path: "login",
    component: LoginComponent
  },
  {
    path: "error/:errorId",
    component: ErrorComponent
  },
  {
    path: "logout",
    component: LogoutComponent
  },
  {
    path: "home",
    data: {
      breadcrumb: "Home"
    },
    component: HomeComponent,
   }
   children: [
     {
        path: "monthlyreport/:type",
        data: {
          breadcrumb: "Reports"
        },
        component: MonthlyReportComponent,
        canActivate: [AuthGuardService]
      }
    ]
 ];

I believe you should rewrite your code as the following: 我相信您应该按照以下方式重写代码:

  {
    path: "home",
    data: {
      breadcrumb: "Home"
    },
    component: HomeComponent,
    children: [
          {
            path: "monthlyreport/:type",
            data: {
              breadcrumb: "Reports"
            },
            component: MonthlyReportComponent,
            canActivate: [AuthGuardService]
          }
        ]
   }

 ];

Which will bring the structure of the object in line with the example used in the Angular documentation here 这将使对象的结构与此处Angular文档中使用的示例一致

const crisisCenterRoutes: Routes = [
  {
    path: 'crisis-center',
    component: CrisisCenterComponent,
    children: [
      {
        path: '',
        component: CrisisListComponent,
        children: [
          {
            path: ':id',
            component: CrisisDetailComponent
          },
          {
            path: '',
            component: CrisisCenterHomeComponent
          }
        ]
      }
    ]
  }
];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM