在调用bootstrap()之前,如何访问angular2的Http服务?

时间:2021-05-02 16:01:01

I need to figure out if the user is authenticated or not so when the root component is bootstrapped it will forward the user to /signin or load whatever page they requested. (I plan on doing that by injecting a service with a boolean if it needs to sign on before making any requests).

我需要弄清楚用户是否经过身份验证,因此当root组件被引导时,它会将用户转发到/ signin或加载他们请求的任何页面。 (如果在发出任何请求之前需要登录,我计划通过注入带有布尔值的服务来实现这一点)。

Angular's docs mention a "Platform Injector" or a "Root Injector". Can I access this to get a Http object? or is it created when bootstrap is called and I need to create my own injector from scratch to get Http?

Angular的文档提到了“Platform Injector”或“Root Injector”。我可以访问它来获取Http对象吗?或者它是在调用bootstrap时创建的,我需要从头开始创建自己的注入器来获取Http?

1 个解决方案

#1


1  

Before bootstrap the http module is not registered as a valid provider, so you can't use DI to instantiate it.

在引导之前,http模块未注册为有效的提供程序,因此您无法使用DI来实例化它。

It's possible that you can find a way to instantiate it manually by importing Http and do

您可以通过导入Http和do来手动实例化它

var http = new Http(..)

var http = new Http(..)

but you would have to satisfy the input arguments and I am not sure if it's recommended to try to work with modules before the application is in a stable bootstrapped state.

但是你必须满足输入参数,我不确定在应用程序处于稳定的自举状态之前是否建议尝试使用模块。

To keep this simple my recommendation would be to do the pre angular check using some other simple http implementation like jquery etc

为了保持这个简单,我的建议是使用其他简单的http实现(如jquery等)进行预角度检查

#1


1  

Before bootstrap the http module is not registered as a valid provider, so you can't use DI to instantiate it.

在引导之前,http模块未注册为有效的提供程序,因此您无法使用DI来实例化它。

It's possible that you can find a way to instantiate it manually by importing Http and do

您可以通过导入Http和do来手动实例化它

var http = new Http(..)

var http = new Http(..)

but you would have to satisfy the input arguments and I am not sure if it's recommended to try to work with modules before the application is in a stable bootstrapped state.

但是你必须满足输入参数,我不确定在应用程序处于稳定的自举状态之前是否建议尝试使用模块。

To keep this simple my recommendation would be to do the pre angular check using some other simple http implementation like jquery etc

为了保持这个简单,我的建议是使用其他简单的http实现(如jquery等)进行预角度检查