如何从npm定义自定义角度模块?

时间:2017-09-17 21:32:28

标签: angular npm node-modules angular-universal

我已经创建了一个npm模块并成功发布了它。 现在我想弄清楚如何将它包含在我的应用程序中。 我正在使用webpack进行编译,似乎无法正确完成。 这是一种角度通用设置。

Angular universal的回购是:

https://github.com/angular/universal-starter/tree/master/custom-webpack

以下是模块的git repo: https://github.com/ChrisTarasovs/angular2-ui-kit

在app.module.ts的项目中我做了

import { BoxModule } from 'ng-universal-ui-kit';

@NgModule({
  imports: [BoxModule  ],
  declarations: [ AppComponent, HomeView ],
  exports: [ AppComponent ]
})
export class AppModule {}

我得到的错误是:

Error: Cannot find module 'ng-universal-ui-kit'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.defineProperty.value (/Users/christarasovs/Desktop/Projects/ng2-uni-webpack/custom-webpack/dist/server.js:72955:18)
    at __webpack_require__ (/Users/christarasovs/Desktop/Projects/ng2-uni-webpack/custom-webpack/dist/server.js:26:30)
    at Object.<anonymous> (/Users/christarasovs/Desktop/Projects/ng2-uni-webpack/custom-webpack/dist/server.js:72874:78)
    at __webpack_require__ (/Users/christarasovs/Desktop/Projects/ng2-uni-webpack/custom-webpack/dist/server.js:26:30)
    at Object.<anonymous> (/Users/christarasovs/Desktop/Projects/ng2-uni-webpack/custom-webpack/dist/server.js:69632:70)
    at __webpack_require__ (/Users/christarasovs/Desktop/Projects/ng2-uni-webpack/custom-webpack/dist/server.js:26:30)
[nodemon] app crashed - waiting for file changes before starting...

我的Package.JSON文件是:

{
  "name": "ng-universal-demo",
  "version": "4.0.0",
  "main": "dist/server.js",
  "repository": {
    "type": "git",
    "url": "https://github.com/angular/universal-starter.git"
  },
  "contributors": [
    "AngularClass <hello@angularclass.com>",
    "PatrickJS <patrick@angularclass.com>",
    "Jeff Whelpley <jeff@gethuman.com>",
    "Jeff Cross <crossj@google.com>",
    "Mark Pieszak <mpieszak84@gmail.com>",
    "Jason Jean <jasonjean1993@gmail.com>"
  ],
  "scripts": {
    "start": "npm run build && npm run server",
    "build": "webpack",
    "build:aot": "webpack --env.aot --env.client & webpack --env.aot --env.server",
    "build:prod": "webpack --env.aot --env.client -p & webpack --env.aot --env.server",
    "prebuild": "npm run clean",
    "prebuild:aot": "npm run clean",
    "prebuild:prod": "npm run clean",
    "clean": "rimraf dist",
    "server": "nodemon dist/server.js",
    "watch": "webpack --watch"
  },
  "engines": {
    "node": ">=6.0.0"
  },
  "license": "MIT",
  "dependencies": {
    "@angular/animations": "^4.0.0",
    "@angular/common": "^4.0.0",
    "@angular/compiler": "^4.0.0",
    "@angular/core": "^4.0.0",
    "@angular/http": "^4.0.0",
    "@angular/platform-browser": "^4.0.0",
    "@angular/platform-browser-dynamic": "^4.0.0",
    "@angular/platform-server": "^4.0.0",
    "@angular/router": "^4.0.0",
    "@nguniversal/express-engine": "^1.0.0-beta.2",
    "express": "^4.15.2",
    "ng-universal-ui-kit": "^1.1.0",
    "rxjs": "^5.4.0",
    "serialize-javascript": "^1.3.0",
    "xhr2": "^0.1.4",
    "zone.js": "^0.8.10"
  },
  "devDependencies": {
    "@angular/compiler-cli": "^4.0.0",
    "@ngtools/webpack": "^1.3.1",
    "@types/express": "^4.0.35",
    "@types/node": "^7.0.18",
    "html-webpack-plugin": "^2.28.0",
    "nodemon": "^1.11.0",
    "raw-loader": "^0.5.1",
    "rimraf": "^2.6.1",
    "script-ext-html-webpack-plugin": "^1.7.1",
    "typescript": "^2.3.2",
    "webpack": "3.3.0",
    "webpack-merge": "^4.1.0",
    "webpack-node-externals": "^1.6.0"
  }
}

我的应用组件是:

import { Component, OnInit } from '@angular/core';
import { TransferState } from '../modules/transfer-state/transfer-state';
import { REQUEST } from '@nguniversal/express-engine/tokens';

@Component({
  selector: 'demo-app',
  template: `
    <Box>Lovely Wuhuu</Box>
    <h1>Universal Demo using Angular</h1>
    <a routerLink="/">Home</a>
    <a routerLink="/lazy">Lazy</a>
    <router-outlet></router-outlet>
  `,
  styles: [
    `h1 {
      color: green;
    }`
  ]
})
export class AppComponent implements OnInit {
  constructor(private cache: TransferState) { }

  ngOnInit() {
    // This is an example
    this.cache.set('message', 'Hello World');
  }
}

1 个答案:

答案 0 :(得分:0)

你不应该在AppModule中声明它。您发布到npm的模块应该在Box的{​​{1}}中列出exports

然后,当您导入BoxModule时,您将能够使用此组件。

这几乎是错误消息所说的;所以不确定是否/为什么你还没有尝试过。