角度延迟加载不使用直接URL

时间:2017-12-16 01:57:54

标签: angular webpack

我正在运行.net Core + webpack 3 + Angular 4。

我的延迟加载在应用程序内部使用时效果很好,就像通过导航栏一样 但是当我尝试直接访问延迟加载的URL时失败。

fail: Microsoft.AspNetCore.NodeServices[0]
      ERROR { Error: Uncaught (in promise): Error: Cannot find module './components/members/members.module.ngfactory'.
      Error: Cannot find module './components/members/members.module.ngfactory'.
          at /root/myApp/bin/Release/netcoreapp2.0/publish/ClientApp/dist/vendor.js:35933:9
          at ZoneDelegate.module.exports.ZoneDelegate.invoke (/root/myApp/bin/Release/netcoreapp2.0/publish/ClientApp/dist/vendor.js:92811:26)
          at Object.onInvoke (/root/myApp/bin/Release/netcoreapp2.0/publish/ClientApp/dist/vendor.js:14833:33)
          at ZoneDelegate.module.exports.ZoneDelegate.invoke (/root/myApp/bin/Release/netcoreapp2.0/publish/ClientApp/dist/vendor.js:92810:32)
          at Zone.module.exports.Zone.run (/root/myApp/bin/Release/netcoreapp2.0/publish/ClientApp/dist/vendor.js:92561:43)
          at /root/myApp/bin/Release/netcoreapp2.0/publish/ClientApp/dist/vendor.js:93238:57
          at ZoneDelegate.module.exports.ZoneDelegate.invokeTask (/root/myApp/bin/Release/netcoreapp2.0/publish/ClientApp/dist/vendor.js:92844:31)
          at Object.onInvokeTask (/root/myApp/bin/Release/netcoreapp2.0/publish/ClientApp/dist/vendor.js:14824:33)
          at ZoneDelegate.module.exports.ZoneDelegate.invokeTask (/root/myApp/bin/Release/netcoreapp2.0/publish/ClientApp/dist/vendor.js:92843:36)
          at Zone.module.exports.Zone.runTask (/root/myApp/bin/Release/netcoreapp2.0/publish/ClientApp/dist/vendor.js:92611:47)

... 是否有针对此问题或解决方法的已知修复程序,因此我可以模仿路由器在应用程序内部工作的方式并将请求“重定向”到延迟加载的模型

1 个答案:

答案 0 :(得分:1)

使用哈希位置策略:

RouterModule.forRoot([...], { useHash });

为什么这样做?

在大多数Web服务器(包括IIS)中,哈希前面的部分被视为服务器上实际页面的路径。但该路由实际上只存在于客户端应用程序中。深层链接将首先命中服务器,当然路由不存在,因此出现404错误。使用#position策略修复了由于服务器忽略#之后的部分,因此它从服务器角度正确解析了页面。 Angular会帮助您进入正确的页面。

您需要告诉Web服务器,SPA的深层链接是可以的。这是一个看起来很不错的指南:https://gingter.org/2017/03/20/deep-link-angular-spa-iis/

相关问题