【问题标题】:How to include and use the package如何包含和使用包
【发布时间】:2018-01-19 06:04:26
【问题描述】:

我正在尝试按照我能找到的示例引入 ngx-bootstrap。

这就是我所拥有的

package.json,在 devDependencies
下 “ngx-bootstrap”:“^1.8.1”

System.config.js

  '@ngx-bootstrap':'npm:node_modules/ngx-bootstrap',


  // other libraries
  'rxjs':                      'npm:rxjs',
  'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
  app: {
    main: './main.js',
    'ngx-bootstrap': { format: 'cjs', main: 'bundles/ngx-bootstrap.umd.js', defaultExtension: 'js' },
    defaultExtension: 'js'
  },
  rxjs: {
    defaultExtension: 'js'
  }
}

app.module

import { AlertModule } from 'ngx-bootstrap';
@NgModule({
  imports: [
      BrowserModule,
      DevBlogModule,
      AlertModule.forRoot(),
      HomeModule
  ],
  declarations: [
    AppComponent
  ],
  bootstrap: [ AppComponent ]
})
export class MainModule { }

在我的 app.component.ts

import { Component } from '@angular/core';
import { AlertModule } from 'ngx-bootstrap';


@Component({
    selector: 'myapp',
    template: '<div> <alert type="success"> this is a test alert</alert>angular2 has been set and here is our first post...</div>'
})

export class AppComponent { }

当我运行时,控制台出现错误,我错过了什么或踩到了什么?

加载资源失败:服务器响应状态为 404 (未找到) :49766/ngx-bootstrap/alert

另外,参考这篇关于 System.js 使用的帖子
How to use ngx-bootstrap in Angular 4 application with SystemJs module system

[ 更新 2 ]
看起来我还有其他问题,在 Visual Studio 的输出窗口中看到 源映射 'data:application/json;base64,';'由于错误,无法正确加载文件“mdha:http://localhost:49766/node_modules/systemjs/dist/system.src.js”。

[ 更新 3 - 以防任何其他 system.js 用户发现此内容 ]
现在我已经开始工作了......
@IlyaSurmay - 请随时在您的网站上为我们的新手使用其中的一些内容。

system.config.js

/**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({


      paths: {
          // paths serve as alias
            'npm:': 'node_modules/',
            'content:': 'content/'
        },
        // 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',
          'moment': 'node_modules:moment/moment.js',

          // other libraries
          'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api',
          'jquery': 'npm:jquery/',
          'lodash': 'npm:lodash/lodash.js',
          'moment': 'npm:moment/',     
          'ngx-bootstrap': 'node_modules/ngx-bootstrap',
          'symbol-observable': 'npm:symbol-observable',
          'rxjs': 'node_modules/rxjs' // added this map section
        },
        // 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'
            },
            'angular2-in-memory-web-api': {
                main: './index.js',
                defaultExtension: 'js'
            },
            'ngx-bootstrap': { format: 'cjs', main: 'bundles/ngx-bootstrap.umd.js', defaultExtension: 'js' },
            'moment': { main: 'moment.js', defaultExtension: 'js' },
            'symbol-observable': { main: 'index.js', defaultExtension: 'js' }
          }

      });
    })(this);

package.json

{
  "author": "me",
  "dependencies": {
    "@angular/cdk": "^2.0.0-beta.8",
    "@angular/common": "~4.3.0",
    "@angular/compiler": "~4.3.0",
    "@angular/core": "~4.3.0",
    "@angular/forms": "~4.3.0",
    "@angular/http": "~4.3.0",
    "@angular/platform-browser": "~4.3.0",
    "@angular/platform-browser-dynamic": "~4.3.0",
    "@angular/router": "~4.3.0",
    "angular-in-memory-web-api": "~0.2.4",
    "core-js": "^2.5.0",
    "moment": "^2.18.1",
    "moment-timezone-all": "^0.5.5",
    "rxjs": "5.0.1",
    "systemjs": "0.19.40",
    "zone.js": "^0.8.4"
  },
  "description": "A starter app using Angular2, Bootstrap CSS for hosting within an ASP.NET MVC web app",
  "devDependencies": {
    "@types/node": "^6.0.85",
    "alertify.js": "^1.0.12",
    "bootstrap": "^3.3.7",
    "font-awesome": "^4.7.0",
    "jquery": "^3.2.1",
    "modernizr": "^3.5.0",
    "ngx-bootstrap": "^1.8.1",
    "systemjs": "^0.19.40"
  },
  "keywords": [],
  "license": "MIT",
  "name": "aspng2",
  "repository": {},
  "version": "1.0.0"
}

【问题讨论】:

    标签: angular ngx-bootstrap


    【解决方案1】:

    System.JS 用户必须直接从 ngx-bootstrap 导入模块,正如官方演示所说http://valor-software.com/ngx-bootstrap/#/modals#usage

    所以把你的导入改成这个import { AlertModule } from 'ngx-bootstrap';

    【讨论】:

    • 你使用的是什么包加载器?
    • Webpack (Angular CLI)。
    • 我仍然收到 404,看到上面我在添加模块和应用程序组件中导入的更改?
    • 好的,经过一番修改后,我可以让它工作了。只是对您网站的请求 - 请为 system.js 用户添加一个部分以便更快地进行操作。
    • 在经历了那个练习之后,我意识到也许我应该探索使用 webpack :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 2014-01-01
    相关资源
    最近更新 更多