好的,我设法更深入地了解了这种行为的原因并设法解决了问题,将在此处记录:
-
这是在 Angular 10.0.5 上引导的 AngularJS (1.7.x) 应用程序
-
使用ng serve --configuration=dev 部署应用程序使其在配置不同 index.ts 并将所有内容从 *.js 转换为 *.ts 的正常漫长过程后按预期工作
-
问题的原因位于angular.json中:
{
"projects":{
[project_name]:{
"architect":{
"build":{
"configurations":{
"production":{
...
"optimization":true <------- THIS
}
...
}}}}}}
优化标志有效地使包非常轻量级,ng build --prod 可以正常编译。但正如我在问题中提到的,问题发生在运行时:路由“加载”但 HTML 永远不会为任何配置的路由呈现,因此任何控制器都运行。
在深入调查中,optimization 标志告诉 angular-cli webpack 在 JS 上运行缩小,根据过去的经验,由于 mangle,AngularJS 似乎很难接收缩小/丑化处理。过程中,我的问题是原因之一。
Angular-CLI 的 Webpack 运行 TerserPlugin 进行缩小,我需要对其进行配置以避免使用 mangle,Angular 现在允许使用自定义 webpack 配置文件添加规则,这是这样实现的:
- 在项目基础文件夹中创建一个新文件,假设命名为
custom-webpack.config.js
- 在 angular.json 构建中引用此文件:
{
"projects":{
[project_name]:{
"architect":{
"build":{
"builder":"@angular-builders/custom-webpack:browser",
"options":{
"customWebpackConfig": {
"path": "./custom-webpack.config.js"
},
...
}
}}}}}}
- 在文件中,让我们这样做:
const TerserPlugin = require('terser-webpack-plugin');
module.exports = {
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
extractComments: true,
terserOptions: {
mangle: false,
ie8:false,
safari10:false
}
}),
],
}
}
瞧!会发生缩小,但不会发生 mangle,应用程序将按预期(大部分)减小它的包大小,并且 angular-route 将正常工作。