【问题标题】:How to add css library to project generated with angular-cli@webpack如何将 css 库添加到使用 angular-cli@webpack 生成的项目中
【发布时间】:2016-12-30 01:49:21
【问题描述】:

创建新项目并将其升级到 webpack 版本后,我想添加 bootstrap 的 CSS。

我尝试了文档 [1] 中描述的方法,但它似乎不起作用。

我无法使用 cdn 版本,因为我的用户可能不得不在无法访问外部网络的情况下工作。

[1]https://github.com/angular/angular-cli#global-library-installation

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

.

$ ng --version
angular-cli: 1.0.0-beta.11-webpack.2
node: 5.4.1
os: linux x64

或者我只是不明白应该发生什么? 在dist 目录中的ng build 之后没有CSS 文件,也没有添加到index.html

【问题讨论】:

    标签: css twitter-bootstrap angular webpack angular-cli


    【解决方案1】:

    如果您升级到1.0.0-beta.11-webpack.3 或更高版本,您可以使用angular-cli.jsonapps[0].styles 属性列出要导入的外部样式表。有了这个,您不必向index.html 添加任何内容。

    要从 1.0.0-beta.11-webpack.2 升级到 1.0.0-beta.11-webpack.3,请运行:

    npm uninstall -g angular-cli
    npm cache clean
    npm install -g angular-cli@1.0.0-beta.11-webpack.3
    

    注意:如果在运行 ng version 时遇到 SyntaxError: Unexpected token ... 错误,您可能需要升级到 Node.js 6。详情请见https://github.com/angular/angular-cli/issues/1883

    如果您生成一个新项目并安装 Bootstrap,您的 angular-cli.json 应该如下所示:

    {
      "project": {
        "version": "1.0.0-beta.11-webpack.3",
        "name": "demo"
      },
      "apps": [
        {
          "root": "src",
          "outDir": "dist",
          "assets": "assets",
          "index": "index.html",
          "main": "main.ts",
          "test": "test.ts",
          "tsconfig": "tsconfig.json",
          "prefix": "app",
          "mobile": false,
          "styles": [
            "styles.css",
            "../node_modules/bootstrap/dist/css/bootstrap.min.css"
          ],
          "scripts": [],
          "environments": {
            "source": "environments/environment.ts",
            "prod": "environments/environment.prod.ts",
            "dev": "environments/environment.dev.ts"
          }
        }
      ],
      "addons": [],
      "packages": [],
      "e2e": {
        "protractor": {
          "config": "./protractor.conf.js"
        }
      },
      "test": {
        "karma": {
          "config": "./karma.conf.js"
        }
      },
      "defaults": {
        "styleExt": "css",
        "prefixInterfaces": false,
        "lazyRoutePrefix": "+"
      }
    }
    

    【讨论】:

      【解决方案2】:

      我认为您必须在 node_modules 前面添加 ../,因为 node_modules 文件夹是目录树中的上一级。 像这样: "../node_modules/bootstrap/dist/css/bootstrap.css"

      【讨论】:

      • 我应该包括这个问题。我已经尝试了两个版本(有和没有../)具有相同的效果(嗯......没有效果)。
      • 也许你可以像here一样使用ng2-bootsrap
      • ng2-bootstrap 只添加 js 而没有 CSS,所以安装它没有帮助。并且提供在 quistion 中提供的 CSS 的方法不适用于 webpack
      【解决方案3】:

      您在ng build 期间来自angular-cli.jsonapps[0].styles 属性的所有css 文件都被编译为styles.bundle.js,并且此文件包含在index.html 中。如果您检查此文件,您可以在那里找到所有样式。所以它按预期工作。

      【讨论】:

        猜你喜欢
        • 2017-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-28
        • 1970-01-01
        • 2020-11-21
        • 1970-01-01
        • 2020-04-16
        相关资源
        最近更新 更多