共计 1630 个字符,预计需要花费 5 分钟才能阅读完成。
重现形式:
npm install and then yarn dev:ssr
谬误音讯:Compiled successfully.
Anqular Universal Live Development Server is Listening on http://localhost:4201
ERROR Error: Uncaught (in promise): Error: RouterModule.forRoot() called twice. Lazy Loaded modules should use RouterModule.forChild() instead.
Error: RouterModule.forRoot() called twice. Lazy Loaded modules should use RouterModule.forChild() instead
at Object.provideForRootGuard [as useFactory] (C:\Code\sony20210526\dist\spartcusstore-server\main. js:115752:15)
at Object. factory (C:\Code\sony20210526\dist\spartacusstore-server\main.js:64140:28)
at R3Injector.hydrate (C:\Code\sony20210526\dist\spartacusstore-server\main.js:64007:63)
at R3Injector.get (C:\Code\sony20210526\dist\spartacusstore-server\main.js:63757:33)
at injectInjectorOnly (C:\Code\sony20210526\dist\spartacusstore-server\main.js:49630:33)
at Module. Dinject (C:\Code\sony20210526\dist\spartacusstore-server\main.js:49640:57)
SAP Spartacus 版本号:2.1.7
剖析
出问题的 homeModule:
HomeModule is importing B2cStorefrontModule, which shouldn’t be imported twice in any scenario, especially shouldn’t be lazy-loaded.
B2bStorefrontModule 不应该被加载两次,也不应该以懒加载的形式被加载。
但事实上,该 module 被反复加载了屡次:
Any lazy-loaded module should only import the most necessary modules to avoid additional performance impact, and it’s especially important for recipe modules like B2cStorefrontModule (the complete b2c storefront recipe).
Custom Module 里如果想提供配置,倡议应用如下形式:
ConfigModule.withConfig()
orprovideConfig
utility/#page-title).
如果用户有大量的自定义逻辑,不举荐应用形如 B2cStorefrontModule 的 recipe module, 而是 building individually tailored storefronts by importing only modules that are needed.
并且,b2cStorefrontModule 在 3.x 版本被设置为 deprecated 状态,并且未来会被移除, in favor of a standardized reference structure.
更多 Jerry 的原创文章,尽在:” 汪子熙 ”: