Angular 2 - 将模块移动到它们自己的项目中

7

我正在尝试使用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
2个回答

2
您可以拥有以下目录结构:

您可以拥有以下目录结构:

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


0

我终于弄清楚了如何使用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 文件中

希望这能帮助其他试图实现这种设置的人。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接