【问题标题】:How can i setup default route in angular4?如何在 angular4 中设置默认路由?
【发布时间】:2018-06-08 05:29:01
【问题描述】:

嗨,我是这个角度的新手,我想在我的应用程序中设置默认路由 下面是我的 app.routing.ts 从 '@angular/router' 导入 { Routes, RouterModule };

import { HomeComponent } from './home/index';
import { LoginComponent } from './login/index';
import { RegisterComponent } from './register/index';
import { AuthGuard } from './_guards/index';

const appRoutes: Routes = [
    { path: '', component: HomeComponent, canActivate: [AuthGuard] },
    { path: 'login', component: LoginComponent },
    { path: 'register', component: RegisterComponent },

    // otherwise redirect to home
    { path: '**', redirectTo: '' }
];

export const routing = RouterModule.forRoot(appRoutes);

目前它正在加载 LoginComponent 我想将其更改为 RegisterComponent 我该怎么做 谁能帮我继续前进

下面是我的 app.module.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule }    from '@angular/forms';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';

// used to create fake backend
import { fakeBackendProvider } from './_helpers/index';

import { AppComponent }  from './app.component';
import { routing }        from './app.routing';

import { AlertComponent } from './_directives/index';
import { AuthGuard } from './_guards/index';
import { JwtInterceptor } from './_helpers/index';
import { AlertService, AuthenticationService, UserService } from './_services/index';
import { HomeComponent } from './home/index';
import { LoginComponent } from './login/index';
import { RegisterComponent } from './register/index';

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        HttpClientModule,
        routing
    ],
    declarations: [
        AppComponent,
        AlertComponent,
        HomeComponent,
        LoginComponent,
        RegisterComponent
    ],
    providers: [
        AuthGuard,
        AlertService,
        AuthenticationService,
        UserService,
        {
            provide: HTTP_INTERCEPTORS,
            useClass: JwtInterceptor,
            multi: true
        },

        // provider used to create fake backend
        fakeBackendProvider
    ],
    bootstrap: [AppComponent]
})

export class AppModule { }

【问题讨论】:

  • { path: '**', redirectTo: 'register' } 是您要找的吗?
  • @eminlala 我试过这个 { path: '**', redirectTo: 'register' } 但仍在加载登录页面
  • 在测试时输入什么作为 URL?另外,如果您也可以为AuthGuard 添加代码,那就太好了。

标签: javascript angular


【解决方案1】:

在您的AuthGard 类canActivate 方法中,如果是false 答案,您可以简单地将客户端重定向到/register。

export class AuthGuard {

  constructor( private authService : AuthService, private router : Router ) {
  }

  canActivate( route : ActivatedRouteSnapshot, state : RouterStateSnapshot ) {
    if(this.authService.isLoggedIn()) 
      return true;
    else // This:
      this.router.navigate(['DESIRED/PATH']);
  }
}

【讨论】:

    【解决方案2】:

    请更改您的应用程序路由顺序并添加 pathMatch 属性。

    const appRoutes: Routes = [
        { path: 'login', component: LoginComponent },
        { path: 'register', component: RegisterComponent },
        { path: '', component: HomeComponent, canActivate: [AuthGuard], pathMatch: 'full' },
    
        // otherwise redirect to home
        { path: '**', redirectTo: '' }
    ];
    

    【讨论】:

      【解决方案3】:

      你可以这样使用,

      // otherwise redirect to home
      { path: '**', redirectTo: '/' }
      

      【讨论】:

        猜你喜欢
        • 2017-01-06
        • 2021-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-02
        • 2018-01-14
        相关资源
        最近更新 更多