角延迟加载模块从错误的URL加载 [英] Angular lazy loaded module is loaded from wrong url

查看:51
本文介绍了角延迟加载模块从错误的URL加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Angular中,我添加了一个新的管理模块,该模块在用户访问管理路由时被延迟加载.

In Angular I added a new admin module which is lazy loaded when the user visits the admin routes.

{
    path: 'admin',
    loadChildren: './admin/admin.module#AdminModule',
}

该模块位于dist文件夹中,但浏览器无法解析模块.js文件,因为它的位置不正确.

The module is present in the dist folder but the browser can't resolve the module .js file because it is looking in the wrong place.

从构建日志开始,模块是按角度构建的:

The module is build by angular as of the build log:

chunk {admin-admin-module} admin-admin-module.js, admin-admin-module.js.map (admin-admin-module) 293 kB  [rendered]

所有构建文件( main.js polyfills.js 等)都存储在名为 browser 的目录中,因此请求URL应该是 http://localhost:8000/browser/admin-admin-module.js ,但请求URL是 http://localhost:8000/admin-admin-module.js .该模块文件是唯一未正确加载的文件.

All build files (main.js, polyfills.js etc.) are stored in a directory named browser so the request URL should be http://localhost:8000/browser/admin-admin-module.js but instead the request URL is http://localhost:8000/admin-admin-module.js. This module file is the only file that is incorrectly loaded.

我似乎无法弄清楚为什么它突然想从应用程序的根目录而不是其他 *.js 文件的加载位置加载此模块./p>

I can't seem to figure out why it suddenly wants to load this module from the root of the application and not in the location the rest of the *.js files are loaded from.

推荐答案

如果像在这种情况下那样在本地网络服务器上运行应用程序,则必须在angular.json文件中设置 baseHref 属性使用域子路径

If you run your app in a local webserver like in your case you have to set the baseHref attribute in your angular.json file to work with a domain sub-path

将其附加到您的构建命令,例如-base-href =/browser/

append it to your build command like --base-href=/browser/

这篇关于角延迟加载模块从错误的URL加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆