如何从webpack angular2应用程序中的node_modules导入CSS

假设我们从以下入门包开始:https : //github.com/angularclass/angular2-webpack-starter

之后npm installnpm run start一切正常。

我想添加一个外部CSS模块,例如bootstrap 4的CSS(仅CSS)。(我知道bootstrap有一个bootstrap-loader,但是现在我要的是通用解决方案,所以请在这里考虑bootstrap 4,因为它可能是通过npm可用的任何其他css模块)。

我通过npm安装bootstrap: npm install bootstrap@4.0.0-alpha.4 --save

首先,我认为添加import 'bootstrap/dist/css/bootstrap.css';到vendor.browser.ts文件就足够了

但事实并非如此。

我应该怎么做才能找到合适的解决方案?

我不要求的解决方案:

  1. “将外部css模块复制到资产文件夹,然后从那里使用它”
    • 我正在寻找与npm软件包一起使用的解决方案。
  2. “将bootstrap-loader用于webpack”
    • 如上所述,我正在寻找一个通用的解决方案,引导程序只是这里的一个示例。
  3. “使用另一个堆栈”
    • 我正在上面提到的确切的入门包中寻找解决方案。
JinJin2020/03/23 21:55:06

因此,这是一种CSS使用angular-cli我认为最方便的方法导入各种文件的方法

Basically, you can refer to the CSS files (order is important if you will be overriding them) in the config and angular-cli will take care of the rest. For instance, you might want to include a couple of styles from node-modules, which can be done as follows:

"styles": [
    "../node_modules/font-awesome/css/font-awesome.min.css",
    "../node_modules/primeng/resources/primeng.min.css",
    "styles.css"
  ]

A sample full-config might look like this:

.angular-cli.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "name": "my-angular-app"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
      "styles": [
        "../node_modules/font-awesome/css/font-awesome.min.css",
        "../node_modules/primeng/resources/primeng.min.css",
        "styles.css"
      ],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "project": "src/tsconfig.app.json",
      "exclude": "**/node_modules/**"
    },
    {
      "project": "src/tsconfig.spec.json",
      "exclude": "**/node_modules/**"
    },
    {
      "project": "e2e/tsconfig.e2e.json",
      "exclude": "**/node_modules/**"
    }
  ],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "scss",
    "component": {}
  }
}