如何在 Angular 4 中将样式导出为 css 文件 [英] How to Export styles as css file in Angular 4

查看:34
本文介绍了如何在 Angular 4 中将样式导出为 css 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Angular 4 的新手,我想将我的样式导出为 css 文件.当我提供或构建项目时,它会创建一个 style.bundle.js 文件并将 css' 添加为 html 样式元素.但我想导出为样式表文件并使用 link 元素加载它.

I am new in Angular 4 and I want to export my styles as css file. When I serve or build the project, it creates a style.bundle.js file and add css' as html style element. But I want to export as stylesheet file and load it with link element.

我的 Angular-cli.json :

My Angular-cli.json :

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "name": "ares"
  },
  "apps": [
    {
      "name":"desktop",
      "root": "src",
      "outDir": "dist/desktop",
      "assets": [
        "assets",
        "favicon.ico",
        "views/desktop"
      ],
      "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/materialize-css/dist/css/materialize.css",
        "../node_modules/font-awesome/css/font-awesome.css",
        "assets/desktop/css/styles.scss",
        "views/desktop/trader/first/assets/css/theme.scss"
      ],
      "scripts": [
        "../node_modules/jquery/dist/jquery.js",
        "../node_modules/hammerjs/hammer.js",
        "../node_modules/materialize-css/dist/js/materialize.js"
      ],
      "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": {}
  }
}

推荐答案

当您运行 ng buildng serve 时,它总是会创建一个类似 styles 的包.bundles.js 只是为了在开发构建上节省一些时间.另一方面,如果你执行 ng build --prod 它会产生 styles.bundle.css.

When you running ng build or ng serve it always creates a bundle like styles.bundles.js just for sake of saving some time on the dev build. From another hand, if you do ng build --prod it will produce styles.bundle.css.

如果您希望为您的开发构建生成 styles.bundle.css 则只需添加 --extract-css 标志,例如 ng serve --extract-css.

If you want styles.bundle.css to be produced for your dev build then just add --extract-css flag e.g ng serve --extract-css.

以下是有关默认 flags 的更多详细信息,可能会有所帮助:

Here is some more details on default flags that might be helpful:

这篇关于如何在 Angular 4 中将样式导出为 css 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆