经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » AngularJS » 查看文章
使用angular-cli webpack创建多个包的方法
来源:jb51  时间:2018/10/17 8:45:07  对本文有异议

当我使用angular-cli构建项目时,它会将所有项目文件捆绑到一个主要的主包中.

我在应用程序中使用了懒惰路由,一旦应用程序加载,我就可以导航.

有没有一种方法可以将主包分为多个基于懒惰加载的路由模块的文件?

下面是角度-cli.json中的配置

  1. {
  2. "project": {
  3. "version": "1.0.0-beta.15",
  4. "name": "maddy-test-project"
  5. },
  6. "apps": [
  7. {
  8. "root": "src",
  9. "outDir": "dist",
  10. "assets": "styles/content",
  11. "index": "default.htm",
  12. "main": "main.ts",
  13. "test": "test.ts",
  14. "tsconfig": "tsconfig.json",
  15. "prefix": "",
  16. "mobile": false,
  17. "styles": [
  18. "styles.less"
  19. ],
  20. "scripts": [
  21. "styles/wfa-myriad-pro-typekit.js"
  22. ],
  23. "environments": {
  24. "source": "environments/environment.ts",
  25. "dev": "environments/environment.ts",
  26. "prod": "environments/environment.prod.ts"
  27. }
  28. }
  29. ],
  30. "addons": [],
  31. "packages": [],
  32. "e2e": {
  33. "protractor": {
  34. "config": "./protractor.conf.js"
  35. }
  36. },
  37. "test": {
  38. "karma": {
  39. "config": "./karma.conf.js"
  40. }
  41. },
  42. "defaults": {
  43. "styleExt": "less",
  44. "prefixInterfaces": false
  45. }
  46. }

下面是package.json

  1. {
  2. "name": "maddy-test-project",
  3. "version": "0.0.1",
  4. "license": "MIT",
  5. "angular-cli": {},
  6. "scripts": {
  7. "start": "ng serve",
  8. "lint": "tslint \"src/**/*.ts\"",
  9. "test": "ng test",
  10. "pree2e": "webdriver-manager update",
  11. "e2e": "protractor"
  12. },
  13. "private": true,
  14. "dependencies": {
  15. "@angular/common": "2.0.0",
  16. "@angular/compiler": "2.0.0",
  17. "@angular/core": "2.0.0",
  18. "@angular/forms": "2.0.0",
  19. "@angular/http": "2.0.0",
  20. "@angular/platform-browser": "2.0.0",
  21. "@angular/platform-browser-dynamic": "2.0.0",
  22. "@angular/router": "3.0.0",
  23. "d3": "^4.2.3",
  24. "jquery": "^3.1.0",
  25. "lodash": "^4.15.0",
  26. "moment": "^2.15.0",
  27. "core-js": "^2.4.1",
  28. "rxjs": "5.0.0-beta.12",
  29. "toastr": "^2.1.2",
  30. "ts-helpers": "^1.1.1",
  31. "zone.js": "^0.6.23",
  32. "bootstrap-daterangepicker": "^2.1.24"
  33. },
  34. "devDependencies": {
  35. "@types/d3": "^3.5.35",
  36. "@types/google-maps": "^3.1.27",
  37. "@types/jasmine": "^2.2.30",
  38. "@types/jquery": "^1.10.31",
  39. "@types/lodash": "^4.14.34",
  40. "@types/toastr": "^2.1.29",
  41. "angular-cli": "1.0.0-beta.15",
  42. "codelyzer": "~0.0.26",
  43. "jasmine-core": "2.4.1",
  44. "jasmine-spec-reporter": "2.5.0",
  45. "karma": "1.2.0",
  46. "karma-chrome-launcher": "^2.0.0",
  47. "karma-cli": "^1.0.1",
  48. "karma-jasmine": "^1.0.2",
  49. "karma-remap-istanbul": "^0.2.1",
  50. "protractor": "4.0.5",
  51. "ts-node": "1.2.1",
  52. "tslint": "3.13.0",
  53. "typescript": "2.0.2"
  54. }
  55. }

提前致谢!!

它是NgModule和RouterModule.forChild()的作用.这是一个非常好的文章,用于启动大型角度2模块化应用开发: http://blog.angular-university.io/angular2-ngmodule/

The first thing that we need to do is to remove every mention of the Home component or the HomeModule from the App component and the main routing configuration:
We can see here that the App component no longer imports HomeModule, instead the routing config uses loadChildren to say that if /home or any other url starting with it gets hit, then the file home.module should be loaded via an Ajax call.

很快,为了在一个懒惰模块中移动一些逻辑和组件,您可以运行以下命令:

  1. ng g module child --routing

然后angular-cli将生成一个NgModule(app / child / child.module.ts)和一个子路由器配置(app / child / child-routing.module.ts).

延迟加载此子路由器的路由将是:

  1. { path: 'child', loadChildren: 'app/child/child.module#ChildModule' }

最后用一个约束移动你想要的ChildModule:其他模块(作为AppModule)将无法使用任何ChildModule依赖项(例如服务).如果你需要它,一个很好的做法是创建一个共享模块.

http://stackoverflow.com/questions/39619003/creating-multiple-bundles-using-angular-cli-webpack

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号