2017-03-01 85 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

解决方案,非常感谢你! –