Bootstrap 4图标在Angular 4项目中无法显示

14

我正在开发一个使用Bootstrap 4和Sass的Angular 4项目,想要使用glyphicons,但是当我使用以下代码时:

<button class="btn btn-sm btn-primary"><span class="glyphicon glyphicon-thumbs-up"></span>Like</button>
例如,当制作“喜欢”按钮时,图标不显示。
我配置了项目以使用“ng-serve”命令编译SCSS样式表,我的“angular-cli.json”文件如下:
{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "name": "front-end-informatorio"
  },
  "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/bootstrap/scss/bootstrap.scss",
        "../node_modules/bootstrap/scss/main.scss"

      ],
      "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": {}
  }
}

我尝试在我的index.html中链接它:

<link data-require="bootstrap-css@3.3.6" data-semver="3.3.6" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css"/>

它使图标工作了,但也改变了我的整个项目风格。

我如何在不重叠样式的情况下实现它?


请查看此答案: https://dev59.com/yVoU5IYBdhLWcg3wAjjV#49920875 不错的解决方案。 - El7or
1个回答

32

Bootstrap 4不再支持Glyphicons图标(详见:https://getbootstrap.com/docs/4.0/migration/#components),这意味着您需要使用其他的图标字体,例如Font Awesome

Font Awesome快速入门

  1. 入门页面获取脚本
  2. 将其粘贴到<head>
  3. 开始使用!FontAwesome.com上查找图标
  4. 探索文档,发挥Font Awesome的全部潜力!

示例:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Bootstrap + Font Awesome</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css">
    <script defer src="https://use.fontawesome.com/releases/v5.0.8/js/all.js"></script>
  </head>
  <body>
    <div class="container">
      <!-- display 4 class makes the font size increase -->
      <h1 class="display-4"><i class="fab fa-stack-overflow"></i></h1>
      <p>That was easy! <i class="far fa-thumbs-up"></i></p>
      <button type="button" class="btn btn-primary">Works also with buttons! <i class="fas fa-link"></i></button>
    </div>
  </body>
</html>

Angular字体图标组件

Font Awesome团队正在开发一个Angular字体图标组件。目前处于预发布状态,但您可以在此处找到它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接