如何导入bootstrap 3.3.7模块 - 用于angular2组件

时间:2016-11-03 18:21:13

标签: angular twitter-bootstrap npm angular-cli

我在导入bootstrap 3.3.7模块时遇到问题并使用它:

package.json 中:我有bootstrap 3.3.7作为依赖:

{
  "name": "plan",
  "version": "0.0.0",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
      "start": "ng serve",
      "lint": "tslint \"src/**/*.ts\"",
      "test": "ng test",
      "pree2e": "webdriver-manager update",
      "e2e": "protractor"
  },
  "private": true,
  "dependencies": {
      "@angular/common": "~2.1.0",
      "@angular/compiler": "~2.1.0",
      "@angular/core": "~2.1.0",
      "@angular/forms": "~2.1.0",
      "@angular/http": "~2.1.0",
      "@angular/platform-browser": "~2.1.0",
      "@angular/platform-browser-dynamic": "~2.1.0",
      "@angular/router": "~3.1.0",
      "angular2-text-mask": "^1.0.1",
      "bootstrap": "^3.3.7",
      "core-js": "^2.4.1",
      "font-awesome": "^4.7.0",
      "jquery": "^3.1.1",
      "ng2-bootstrap": "^1.1.16",
      "rxjs": "5.0.0-beta.12",
      "tether": "^1.3.7",
      "ts-helpers": "^1.1.1",
      "zone.js": "^0.6.23"
  },
  "devDependencies": {
      "@types/jasmine": "^2.2.30",
      "@types/node": "^6.0.42",
      "angular-cli": "1.0.0-beta.19-3",
      "codelyzer": "1.0.0-beta.1",
      "jasmine-core": "2.4.1",
      "jasmine-spec-reporter": "2.5.0",
      "karma": "1.2.0",
      "karma-chrome-launcher": "^2.0.0",
      "karma-cli": "^1.0.1",
      "karma-jasmine": "^1.0.2",
      "karma-remap-istanbul": "^0.2.1",
      "protractor": "4.0.9",
      "ts-node": "1.2.1",
      "tslint": "3.13.0",
      "typescript": "~2.0.3",
      "webdriver-manager": "10.2.5"
  }
}

app.modules.ts 中,尝试导入模块时出现以下错误:

错误:无法找到模块../../../node_modules/bootstrap/dist/js/bootstrap 在第16行第27行

enter image description here

我已经在SO中查了一些问题,但它没有帮助

Angular2 webpack: how to import bootstrap css

2 个答案:

答案 0 :(得分:2)

根据https://angular.io/docs/ts/latest/guide/webpack.html#!#loaders

在你的webpack.config.js中你应该

  

教它用加载器将这些文件处理成JavaScript

loaders: [
  {
    test: /\.ts$/
    loaders: 'ts'
  },
  {
    test: /\.css$/
    loaders: 'style!css'
  }
]

你是这样进口的吗?

import 'uiframework/dist/uiframework.css';

答案 1 :(得分:1)

您可以通过自定义angular-cli.json文件将其作为全局库导入,如angular-cli documentation中所述。

将脚本文件添加到apps[0].scripts

"scripts": [
  "../node_modules/bootstrap/dist/js/bootstrap.js"
],

将Bootstrap CSS添加到apps[0].styles数组:

"styles": [
  "../node_modules/bootstrap/dist/css/bootstrap.css"
],