2017-03-01 88 views

回答

3

如果您希望將任何URL重定向到它的https等效項,請將此類作爲服務實現。該類將檢查開發人員模式,以便僅當應用程序部署在產品中時纔會發生重定向。

import {Injectable, isDevMode} from '@angular/core'; 
import {CanActivate, ActivatedRouteSnapshot} from '@angular/router'; 

@Injectable() 
export class IsSecureGuard implements CanActivate { 

    canActivate(route: ActivatedRouteSnapshot): boolean { 
    if (!(isDevMode()) && (location.protocol !== 'https:')) { 
     location.href = 'https:' + window.location.href.substring(window.location.protocol.length); 
     return false; 
    } 
    return true; 
    } 

} 

此警衛必須適用於每條路徑。例如:

{path: 'mypath', component: MyPathComponent, canActivate: [IsSecureGuard]} 
+0

解決方案,非常感謝你! –