Angular 2 + Heroku,总是重定向到https://而不是使用http://

问题描述:

我有一个在Heroku中托管的Angular 2的应用程序,我想将所有的http请求重定向到https,什么是正确的方法去做吧?谢谢!Angular 2 + Heroku,总是重定向到https://而不是使用http://

如果您希望将任何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

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