javascript - 我如何在 Angular4 中设置默认路由?

标签 javascript angular

嗨,我是这个 Angular 新手,我想在我的应用程序中设置默认路由 下面是我的 app.routing.ts 从'@angular/router'导入{路由,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 { }

最佳答案

在您的 AuthGardcanActivate 方法中,如果出现 false 答案,您只需将客户端重定向到 /注册

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']);
  }
}

关于javascript - 我如何在 Angular4 中设置默认路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47994379/

相关文章:

javascript - JS onClick 防止输入的默认值

javascript - datepicker解析日期问题

javascript - 如何使用 JS 从 div 元素中删除所有子元素

javascript - 简单的 sum javascript 函数在 IE 中不起作用

angular - 使用 NgRX 实体规范化 Api 响应

angular - 如何在我的 Angular 4 应用程序中使用 angulartics2 (Adobe Analytics) 跟踪事件

javascript - 导入 ngrx 开发工具时无法读取未定义的属性 'whitelist'

javascript - 这个构造在 javascript 中做什么

angular - ionic 2 GET http ://localhost:8100/search/aq? 查询= 404(未找到)

angular - 如何在Angular 8中显示超时错误