Angular 服务器端渲染的一个错误消息 - No provider for InjectionToken REQUEST
錯誤消息:
NullInjectorError: R3InjectorError(AppServerModule)
WindowRef -> InjectionToken SERVER_REQUEST_URL -> InjectionToken SERVER_REQUEST_URL -> InjectionToken SERVER_REQUEST_URL -> InjectionToken REQUEST]:
NullInjectorError: No provider for InjectionToken REQUEST!
Angular universal engine 的文檔:
https://github.com/angular/universal/tree/master/modules/express-engine
服務(wù)器端的 HTTP request 調(diào)用,通過 Injection token 注入到 SSR Application 里:
import { Request } from 'express'; import { REQUEST } from '@nguniversal/express-engine/tokens';@Injectable() export class RequestService {constructor(@Inject(REQUEST) private request: Request) {} }這個 token 在客戶端環(huán)境下不可用。
相關(guān)錯誤:https://github.com/angular/universal-starter/issues/423
https://gitter.im/angular/universal/archives/2018/06/17
REQUEST and RESPONSE won’t exist when you run in the browser context, even when you are running the SSR server
Use @Optional and take care to check whether you’ve got it, and consider using isServerPlatform to detect when you are running on the server side
在瀏覽器上下文環(huán)境運行時,REQUEST 和 RESPONSE 不可用。使用 @Optional 來避免 NullInjectorError 錯誤,以及使用 isServerPlatform 檢測當前的運行環(huán)境。
相關(guān) issue:https://github.com/SAP/spartacus/issues/11016
The SERVER_REQUEST_ORIGIN and SERVER_REQUEST_URL should not be provided in CSR by design.
SERVER_REQUEST_ORIGIN 在文件 ssr-providers.ts 里提供實現(xiàn):
總結(jié)
以上是生活随笔為你收集整理的Angular 服务器端渲染的一个错误消息 - No provider for InjectionToken REQUEST的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微软 Xbox 无线手柄推出新配色:北极
- 下一篇: 增值税计税依据 增值税的计税依据