【问题标题】:Having trouble using an external angular2 module via npm通过 npm 使用外部 angular2 模块时遇到问题
【发布时间】:2017-03-26 11:49:21
【问题描述】:

我正在使用 Angular 2 快速入门并尝试添加第三方组件(具体为 ng2-split-pane)。我用 npm 安装它,并基于它的 plunkr 示例,我有以下 systemjs.config.js:

(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/'
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      app: 'app',

      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
      '@angular/material': 'npm:@angular/material/bundles/material.umd.js',

      // other libraries
      'rxjs':                      'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
      'autobahn':          'npm:@types/autobahn/index.d.ts',
      'ng2-split-pane': 'npm:ng2-split-pane/lib/ng2-split-pane.js',
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        main: './main.js',
        defaultExtension: 'js'
      },
      rxjs: {
        defaultExtension: 'js'
      }
    }
  });
})(this);

以及以下app.module.ts:

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent }  from './app.component';
import { MaterialModule } from '@angular/material';
import { BattleLogComponent } from './battlelog.component';
import { SplitPaneModule } from 'ng2-split-pane';

@NgModule({
  imports:      [ BrowserModule, MaterialModule, SplitPaneModule ],
  declarations: [ AppComponent, BattleLogComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

但我只是收到一条错误消息“app/app.module.ts(7,33): error TS2307: Cannot find module 'ng2-split-pane'。”

我能够很好地加载其他东西(例如材料),我在这里缺少什么?

编辑:目录结构(大部分 node_modules 被省略):

.
├── app
│   ├── app.component.ts
│   ├── app.module.ts
│   ├── app.ng.css
│   ├── app.ng.html
│   ├── autobahn.service.ts
│   ├── battlelog.component.ts
│   ├── battlelog.ng.html
│   └── main.ts
├── favicon.ico
├── index.html
├── js
│   └── autobahn.min.js
├── LICENSE
├── node_modules
│   ├── mkdirp
│   ├── moment
│   ├── ms
│   ├── negotiator
│   ├── ng2-split-pane
│   ├── node-uuid
│   ├── nopt
├── package.json
├── styles.css
├── systemjs.config.extras.js
├── systemjs.config.js
├── tsconfig.json
└── tslint.json

【问题讨论】:

  • ng2-split-pane 是否出现在您的 node_modules 文件夹中?
  • 是的——我验证了 node_modules/ng2-split-pane/lib/ng2-split-pane.js 存在并且有一个 SplitPaneModule 的导出。
  • 您的 node_modules 文件夹在您的项目中位于何处?我们可以得到一个目录树吗?
  • 编辑成主要问题。
  • 试试import 'ng2-split-pane'而不是import { SplitPaneModule } from 'ng2-split-pane';

标签: node.js angular typescript npm


【解决方案1】:

我可以通过使用 Angular-cli 明确说明 ng2-split-pane 的位置来实现此功能:

import { SplitPaneModule } from '../../node_modules/ng2-split-pane/src/ng2-split-pane';

【讨论】:

  • 所以实际上,虽然这个编译它现在无法在我尝试运行页面时加载资源::3000/node_modules/ng2-split-pane/src/ng2-split-pane 加载失败资源:服务器响应状态为 404(未找到)
  • 嗯,这可能是任何数量的路径相关问题。除非您非常喜欢systemjs,否则我会尝试使用始终可爱的angular-cli 重新引导您的应用程序:github.com/angular/angular-cli#installation(使用webpack)安装后在您最喜欢的终端模拟器中使用ng new someApplication 并重新尝试安装@987654326 @。祝你好运!
猜你喜欢
  • 1970-01-01
  • 2022-01-26
  • 2018-07-18
  • 2017-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-07
相关资源
最近更新 更多