2017-10-16 44 views
0

我希望用戶在名稱/密碼爲true時進行日誌記錄,否則留在頁面上。 我正在使用routerLink,但我無法弄清楚如何在不檢查的情況下將路由器重定向到儀表板。 我使用了一個名爲test()的函數:布爾值,如果名稱/密碼有效,則返回true。Angular2條件用於登錄的條件RouterLink

<a class="btn rounded-btn" [routerLink]="['dashboard', 'home']" onClick="test()"> Log in </a> 

這裏是我的login.component:

import { Component, OnInit } from '@angular/core'; 

@Component({ 
    selector: 'app-login', 
    templateUrl: './login.component.html', 
    styleUrls: ['./login.component.scss'] 
}) 
export class LoginComponent implements OnInit { 
    constructor() { } 
    ngOnInit() { } 

    test():boolean{ 

     return false ; 
    } 
} 
+0

使用AuthGuard。在這裏:http://jasonwatmore.com/post/2016/12/08/angular-2-redirect-to-previous-url-after-login-with-auth-guard – Faisal

回答

0

您可以使用路由器的導航,而不是RouterLink,當用戶名和密碼會糾正將導航到儀表板頁面:

這.router.navigate([「/ dashboard」],{some-data:「otherData」});

你可以從角文檔的詳細信息:https://angular.io/guide/router#navigating-back-to-the-list-component

+0

我應該在哪裏申報該路由器? – BadrEddineBen

+0

進樣路由器到構造函數: '進口{ActivatedRoute,路由器}從「@角/路由器;' '構造函數(私人路由器:路由器){ } // constructor'結束 – Amit

+0

感謝的,這是工作 – BadrEddineBen