【问题标题】:angular2 can't import ng2-bootstrapangular2 无法导入 ng2-bootstrap
【发布时间】:2015-12-21 11:50:32
【问题描述】:

我无法导入 ng2-bootstrap

layout.jade

html
 head
title Angular 2 QuickStart
// 1. Load libraries

script(src="/node_modules/angular2/bundles/angular2-polyfills.js")
script(src="/node_modules/systemjs/dist/system.src.js")
//script(src="/node_modules/ng2-bootstrap/ng2-bootstrap.js")
script(src="/node_modules/rxjs/bundles/Rx.js")
script(src="/node_modules/angular2/bundles/angular2.dev.js")

// 2. Configure SystemJS
script.
  System.config({
      packages: {
         app: {
            //format: 'register',
            //defaultExtension: 'js',
            defaultJSExtensions: true
        }
      },
          paths: {
          'ng2-bootstrap/*': 'node_modules/ng2-bootstrap/*.js'
          }
  });
  System.import('app/boot')
  .then(null, console.error.bind(console));
// 3. Display the application
body
    my-app Loading...

表达

 app.use('/node_modules', express.static(__dirname + '/node_modules'));

app.component.ts

 //import "ng2-bootstrap/ng2-bootstrap";

   import {Component} from 'angular2/core';
   import {Alert} from 'ng2-bootstrap/ng2-bootstrap';
   import {firstComponent} from './component/first.component';

   @Component({
     //directives: [Alert],
      directives: [firstComponent,Alert],
      selector: 'my-app',
      template: `
        <h1>My First Angular 2 App</h1>
        <h2> helllo</h2>
        <first></first>
        <alert type="info">ng2-bootstrap hello!</alert>
        `
    })
    export class AppComponent { }

错误控制台

GET http://localhost:3000/node_modules/ng2-bootstrap/components/accordion/accordion 404(未找到)
Error: XHR error (404 Not Found) loading http://localhost:3000/node_modules/ng2-

还有更多与re:无法导入组件相同的错误?

【问题讨论】:

  • ng2-bootstrap,更名为 ngx-bootstrap

标签: angular ng2-bootstrap


【解决方案1】:

我自己也遇到了同样的问题,发现这个链接很有帮助:https://github.com/valor-software/ng2-bootstrap/issues/50#issuecomment-165448962

最相关代码的sn-p:

System.JS 配置:

System.config({
   packages: {
     "app": {
        "defaultExtension": "js"
     },
     "node_modules/ng2-bootstrap": {
        "defaultExtension": "js"
     }
  },
  paths: {
    "ng2-bootstrap":   "node_modules/ng2-bootstrap"
  }
});

然后您可以像这样简单地导入它:

import { Progressbar } from "ng2-bootstrap";
@Component({
    selector: "my-app",
    directives: [
        Progressbar
    ],
    template: "<progressbar></progressbar>" 
})

希望这对您有所帮助,就像对我一样。

【讨论】:

  • 您好,我使用的是 webpack,请问在哪里可以找到 System.JS 文件?
  • @antonio 它被命名为system-config.ts
【解决方案2】:

这对我有用。 在systemjs.config文件下:

map: {
'ng2-bootstrap': 'node_modules/ng2-bootstrap/bundles/ng2-bootstrap.umd.js',
       'moment': 'node_modules/moment/moment.js'
    }

我们需要提到“时刻”,因为 ng2-bootstrap 正在导入时刻。

在 app.modules 中,只需执行以下操作:

import {DatepickerModule} from 'ng2-bootstrap';

还有以下内容:

@NgModule({
    imports: [
        ....,
        ....,
        DatepickerModule
    ],
    declarations: [...],
    providers: [...],
    bootstrap: [AppComponent]
})

【讨论】:

  • 伟大的分析,添加时刻并保持其余行对我有帮助。
【解决方案3】:

我认为当前的答案并不令人满意,因为它们确实可以解决症状并且不能解决根本问题。

我做了什么来解决这个问题:

System.config({
    baseURL: '/node_modules',
    "defaultJSExtensions": true,
    map: {
      app: '/app/'
    }
    paths: {
      "moment":   "moment/moment"
    }
  });

第 1 行:所以我设置了node_modules 我的基本 url,所以我不必逐个包地重写每个导入。

第 2 行:然后我告诉 systemjs 默认查找 JS 文件。

第 3 - 5 行:我将所有以 app 开头的内容映射到 app 目录。所以 SystemJs 不会尝试在 /node_modules 文件夹中找到我自己的文件。

完成。

在第 6 - 7 行,当 SystemJS 尝试直接从 /node_modules/ 加载 momentjs 时,必须完成一个“错误修复”,因此我将其设置为自己的目录。奇怪..

【讨论】:

    【解决方案4】:

    我正在使用最新版本的 angular-cli 2.4.7 (webpack) 并且能够让它工作。

    在你的 Angular 应用中运行它

    npm install ng2-bootstrap bootstrap moment --save
    

    打开以下文件并按照列出的方式添加它们

    在样式数组中插入一个新条目

      # angular-cli.json
      "styles": [
            "styles.css",
            "../node_modules/bootstrap/dist/css/bootstrap.min.css"
          ]
    

    导入所需模块

    # src/app/app.module.ts
    ...
    import { DatepickerModule } from 'ng2-bootstrap';
    ...
    @NgModule({
    ....
    ,
      imports: [
    DatepickerModule.forRoot(),
    ...
    ]
    ...})
    

    你很高兴!

    【讨论】:

      【解决方案5】:
                              **>-------- NG2BOOTSTRAP--------->**
      

      使用这些命令将这些依赖项添加到您的 package.json 中

      1. npm install moment --save
      2. npm install ng2-bootstrap --save

      将您的 System.config.js 修改为以下代码,因为此映射告诉系统加载器在哪里查找代码。

      var map = {
          'app':                        'app', // 'dist',
          '@angular':                   'node_modules/@angular',
           moment:              'node_modules/moment/moment.js',
          'ng2-bootstrap':              'node_modules/ng2-bootstrap',
        };
      
      var packages = {
          'app':                        { main: 'main.js',    defaultExtension: 'js' },
          'ng2-bootstrap':          {defaultExtension: 'js' }
        };
      

      将 Bootstrap 添加到您的 index.html &lt;link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"&gt;

      App.module.ts 从“ng2-bootstrap/ng2-bootstrap”导入{ Ng2BootstrapModule }; 并且,将 Ng2BootstrapModule 添加到 @NgModule 的导入中。

      那么,你可以选择 ng2Bootstrap。

      【讨论】:

      • 您好,我使用的是 webpack,请问在哪里可以找到 System.config.js 文件?
      【解决方案6】:

      在systemjs.config.js packages 变量中,添加以下内容以修复问题

      'ng2-bootstrap':
      {
          format: 'cjs', main: 'bundles/ng2-bootstrap.umd.js', defaultExtension: 'js'
      }
      

      【讨论】:

        【解决方案7】:

        使用以下内容更新 systemjs.config.js 包变量,错误将消失 'ng2-bootstrap': { format: 'cjs', main: 'bundles/ng2-bootstrap.umd.js', defaultExtension: 'js' },// AND THIS

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-07-11
          • 2017-01-05
          • 1970-01-01
          • 2017-08-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-04
          相关资源
          最近更新 更多