【问题标题】:Angular2 - not able to add component from npmAngular2 - 无法从 npm 添加组件
【发布时间】:2016-04-03 12:08:46
【问题描述】:

我刚刚从名为 ng2-easy-table 的 npmjs 下载了 angular 组件(我是这个组件的创建者,所以也许我在创建它时犯了一些错误)。

package.json

{
  "name": "untitled",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "concurrently \"npm run tsc:w\" \"npm run lite\" \"npm run styles\" ",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "lite": "lite-server",
    "typings": "typings"
  },
  "dependencies": {
    "angular2": "2.0.0-beta.13",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.35.0",
    "ng2-easy-table": "0.0.12",
    "node-sass": "^3.4.2",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.2",
    "systemjs": "0.19.24",
    "zone.js": "^0.6.6"
  },
  "devDependencies": {
    "concurrently": "^2.0.0",
    "lite-server": "^2.1.0",
    "typescript": "^1.8.7",
    "typings": "^0.7.5"
  },
  "author": "",
  "license": "ISC"
}

然后我创建了简单的app.component.ts 来添加ng2-easy-table 指令。

app.component.ts

import {Component} from 'angular2/core';

import {bootstrap} from 'angular2/platform/browser';
import {AppComponent} from 'ng2-easy-table/app/app.component';

@Component({
  selector: 'app',
  templateUrl: 'app/index.html',
  directives: [AppComponent]
})

export class IndexComponent { }

bootstrap(IndexComponent, []);

node_modules 中如下所示:

还有System.config

<script>
    System.config({
        packages: {
            app: {
                format: 'register',
                defaultExtension: 'js'
            }
        }
    });
    System.import('app/app.component')
            .then(null, console.error.bind(console));
</script>

但是当我从控制台启动应用程序 npm start 时,我得到了:

GET http://localhost:3002/ng2-easy-table/app/app.component 404(不是 找到)

错误:XHR 错误(404 Not Found)正在加载 http://localhost:3002/ng2-easy-table/app/app.component(...)

编辑

添加@Thierry Templier 提供的 Config.style 后,我得到:

【问题讨论】:

  • 你需要添加你的 System.config,你的问题就在那里。
  • @EricMartinez 完成了,我应该在 system.config 中添加类似 paths: { 'ng2-easy-table/': 'node_modules/ng2-easy-table/' } 的内容吗?

标签: angularjs typescript angular


【解决方案1】:

您需要在 SystemJS 的配置中为该库添加一个地图块:

<script>
    System.config({
        map: {
          'ng2-easy-table': 'node_modules/ng2-easy-table'
        },
        packages: {
            app: {
                format: 'register',
                defaultExtension: 'js'
            },
            'ng2-easy-table': {
                format: 'register',
                defaultExtension: 'js'
            } 
        }
    });
    System.import('app/app.component')
            .then(null, console.error.bind(console));

【讨论】:

  • 我编辑了我的问题。现在我收到http://localhost:3000/node_modules/ng2-easy-table/app/app.component 404 (Not Found)
  • 您可以在您的 systemjs 配置的 packages 块中添加一个条目。我更新了我的答案...
猜你喜欢
  • 2016-11-03
  • 1970-01-01
  • 2016-06-15
  • 1970-01-01
  • 2017-07-31
  • 1970-01-01
  • 2016-12-22
  • 2017-07-20
  • 1970-01-01
相关资源
最近更新 更多