嗨,我是这个角色的新手,我想在我的应用程序中设置默认路线 下面是我的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 { }
答案 0 :(得分: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']);
}
}
答案 1 :(得分:1)
请更改您的应用路由序列并添加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: '' }
];
答案 2 :(得分:0)
你可以这样使用,
// otherwise redirect to home
{ path: '**', redirectTo: '/' }