【问题标题】:Angular 2 - moving modules to their own projectAngular 2 - 将模块移动到自己的项目中
【发布时间】:2017-03-17 22:04:37
【问题描述】:

我正在尝试使用 Angular 2 创建类似于门户的功能,该功能将提供基本导航和全局服务(例如身份验证),但允许其他开发人员创建自己的模块,这些模块从根本上插入到门户中。

我正在使用 angular-cli,并通过路由器(使用 loadChildren)在 app.module 中延迟加载在项目内部创建的模块(参见下面的树)对概念进行了快速验证。每个子模块都有自己的路由器,并且从根本上与父门户(app.module)解耦。

我最终想将这些子模块转移到他们自己的项目中,但完全不知道从哪里开始,而且网上相关的信息似乎很少。如果有人知道示例或可以演示如何设置,我将不胜感激。

编辑:如果可能的话,我想继续使用 Angular-CLI 功能来做到这一点。

这是我的目录结构。模块 1、2 和 3 需要移动到他们自己的项目中。

+-- app
│   +-- app.component.css
│   +-- app.component.html
│   +-- app.component.spec.ts
│   +-- app.component.ts
│   +-- app.module.ts
│   +-- module1
│   │   +-- dataflows
│   │   │   +-- dataflows.component.css
│   │   │   +-- dataflows.component.html
│   │   │   \-- dataflows.component.ts
│   │   +-- module1.component.css
│   │   +-- module1.component.html
│   │   +-- module1.component.ts
│   │   +-- module1.module.ts
│   │   \-- other
│   │       +-- other.component.css
│   │       +-- other.component.html
│   │       \-- other.component.ts
│   +-- index.ts
│   +-- module2
│   │   +-- module2.component.css
│   │   +-- module2.component.html
│   │   +-- module2.component.ts
│   │   \-- module2.module.ts
│   \-- module3
│       +-- dummy1
│       │   +-- dummy1.component.css
│       │   +-- dummy1.component.html
│       │   \-- dummy1.component.ts
│       +-- module3.component.css
│       +-- module3.component.html
│       +-- module3.component.ts
│       +-- module3.module.ts
│       \-- dummy2
│           +-- dummy2.component.css
│           +-- dummy2.component.html
│           \-- dummy2.component.ts
+-- index.html

【问题讨论】:

    标签: angularjs angular angular2-routing


    【解决方案1】:

    你可以有以下目录结构:

    angular
    |
    ---- common_files
    |     |
    |     ----- package.json
    |     |
    |     ----- index.ts
    |     |
    |     ----- catalog1
    |           |
    |           ---- package.json
    |           |
    |           ---- some_file_with_service_model_comopnent.ts
    |           |
    |           ---- index.ts    - this is regular barrel file
    |     |
    |     ----- catalog2
    |
    ---- app1
         |
         ------ package.json
    |
    ---- app2
         |
         ------ package.json
    

    /angular/common_files/

    {
      "name": "common-modules",
      "version": "0.0.1",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "dev": "tsc -w",
        "tsc": "tsc",
        "tsc:w": "tsc -w",
        "pack": "webpack"
      },
      "typings": "./index.d.ts",
      "author": "Maciej Sobala",
      "license": "UNLICENSED",
      "dependencies": {
        "@angular/common": "2.0.0",
        "@angular/compiler": "2.0.0",
        "@angular/core": "2.0.0",
        "@angular/forms": "2.0.0",
        "@angular/http": "2.0.0",
        "@angular/material": "2.0.0-alpha.9-3",
        "@angular/router": "3.0.0",
        "ng2-cookies": "^1.0.2",
        "rxjs": "5.0.0-beta.12",
        "typescript": "2.0.0",
        "typescript-collections": "^1.2.3",
        "zone.js": "^0.6.12"
      },
      "private": "true",
      "devDependencies": {
        "@types/body-parser": "0.0.29",
        "@types/compression": "0.0.29",
        "@types/cookie-parser": "^1.3.29",
        "@types/express": "^4.0.32",
        "@types/express-serve-static-core": "^4.0.33",
        "@types/hammerjs": "^2.0.32",
        "@types/mime": "0.0.28",
        "@types/node": "^6.0.38",
        "@types/core-js": "^0.9.34",
        "webpack": "^1.13.2",
        "webpack-merge": "^0.14.0",
        "angular2-template-loader": "^0.4.0",
        "awesome-typescript-loader": "~1.1.1"
      }
    }
    

    /angular/common_files/index.ts:

    export * from './catalog1/index';
    export * from './catalog2/index';
    

    /angular/common_files/catalog1/package.json:

    {
      "name": "common-modules/catalog1",
      "main": "index.js"
    }
    

    现在您可以使用 npm run tsc 编译您的公共资源。之后,您可以在 app1 和 app2 中重复使用它们:

    npm install ../common/ --save
    

    这将在您的 app1 package.json 中创建条目:

    "dependencies": {
        "common-modules": "file:///Users/my_name/Documents/work/my_project/angular/common_files",
      }
    

    之后,您可以从 app1 和/或 app2 import {something} from 'common-modules/catalog1'; 文件中导入模块

    您还应该查看此链接:https://docs.npmjs.com/private-modules/intro

    【讨论】:

      【解决方案2】:

      我终于想出了如何使用 SystemJS 而不是 Webpack/Angular CLI 来实现这一点。

      我基本上将模块移动到他们自己的项目中,并使用子项目中 tsconfig.json 文件中的 outFile: 属性使用“npm run tsc”。您还必须在 tsconfig.json 文件中设置 "module": "system"

      然后我手动将生成的编译模块复制到主项目的根目录中。

      为了让延迟加载发挥作用,我在路由器配置中使用 loadChildren: 'test.module' 并添加:

      bundles: {
          'test.module.js': ['test.module']
      } 
      

      到我的 systemjs.config.js

      希望这对尝试实现这种设置的其他人有所帮助。

      【讨论】:

        猜你喜欢
        • 2017-03-24
        • 1970-01-01
        • 2017-04-24
        • 1970-01-01
        • 2017-12-25
        • 2015-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多