Angular 8:刷新基本网址时页面重定向到主页

时间:2019-10-22 06:04:07

标签: angular routes

在我的角度项目中,我有一个菜单仪表板,登录时重定向到仪表板,但是当我从URL上删除该仪表板(即)在localhost:4200 / dashboard之前,在删除localhost:4200之后,刷新时我如何导航到仪表板了吗?

{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{ path: 'dashboard', component: DashboardComponent},
{ path: '**', redirectTo: 'login' ,pathMatch: 'full' }

3 个答案:

答案 0 :(得分:0)

https://angular.io/guide/router#the-default-route-to-heroes

您可以按以下方式更改路由器配置,

RouterConfig = [
  { path: '**', redirectTo: '/dashboard'},
  { path: '', component: HomeComponent, canActivate: [AuthGuard] },
  { path: 'login', component: LoginComponent }
];

将此添加到路由器配置中,它应该可以正常工作。

基本路由示例=> https://stackblitz.com/edit/angular-router-basic-example-11fgob

答案 1 :(得分:0)

您可以使用redirectTo

According to the Angular documentation for the default routing

{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{ path: 'dashboard', component: DashboardComponent},
{ path: '**', redirectTo: 'login' ,pathMatch: 'full' }

此路由会将与空路径完全匹配的URL重定向到路径为'/dashboard'.的路由

浏览器刷新后,router加载DashboardComponent,浏览器地址栏显示/dashboard URL。

答案 2 :(得分:0)

按如下所示更改路由器配置。

  import { DashboardComponent} from '..path to your dashboard component';
  import { LoginComponent } from '..path to your login component';

  const routes: Routes = [
  { path: '', redirectTo: '/dashboard' ,pathMatch: 'full'} 
  { path: 'dashboard', component: DashboardComponent}, canActivate:[AuthGuard]}
  { path: 'login', component: LoginComponent },
  ]