【问题标题】:How to set up latest Angular (v15) with Bootstrap (v5) with .scss files如何使用带有 .scss 文件的 Bootstrap (v5) 设置最新的 Angular (v15)
【发布时间】:2023-01-07 17:23:05
【问题描述】:

如何使用引导程序和 SASS(.scss 文件)设置 Angular?

【问题讨论】:

    标签: angular sass bootstrap-5


    【解决方案1】:

    使用ng-bootstrap

    在 CLI 中输入:

    ng new my-ng-bootstrap-app # Create angular app. Choose routing and SCSS options
    ng add @ng-bootstrap/ng-bootstrap # add bootstrap (see notes at bottom of this answer for how bootstrap is added to app)
    ng generate component my-flex # Create a new component to test bootstrap's flex styles
    ng serve # Run the Angular server
    

    这将导致更高版本的 Angular 出错(我认为从 v14 开始)。通过替换解决此问题:

    ./src/styles.scss

    // @import '~bootstrap/scss/bootstrap';
    // Replace above line with
    @import 'node_modules/bootstrap/scss/bootstrap';
    

    现在已经设置了引导程序,我们可以转到https://getbootstrap.com/docs/5.0/layout/grid/ 并复制代码。

    src/app/my-flex/my-flex.component.html

      <!-- Replace the code in here with the example code from https://getbootstrap.com/docs/5.0/layout/grid/#example -->
    <div class="container">
      <div class="row">
        <div class="col">
          Column
        </div>
        <div class="col">
          Column
        </div>
        <div class="col">
          Column
        </div>
      </div>
    </div>
    

    src/app/app.component.html

    <!-- Delete all previous code -->
    <app-my-flex></app-my-flex>
    

    那应该是一切正常。您可以检查浏览器中的开发人员工具,以查看呈现的 DOM 元素是否应用了引导程序样式。

    (仅供参考)ng add @ng-bootstrap/ng-bootstrap 将以下内容添加到:

    源代码/styles.scss

    /* Importing Bootstrap SCSS file. */
    @import '~bootstrap/scss/bootstrap';
    

    src/app/app.module.ts

    import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
    ...
    @NgModule({
      ...
      imports: [
        ...
        NgbModule
      ],
      bootstrap: [AppComponent]
    })
    

    【讨论】:

      猜你喜欢
      • 2022-12-17
      • 2022-06-16
      • 2017-01-04
      • 1970-01-01
      • 1970-01-01
      • 2015-04-16
      • 1970-01-01
      • 2021-12-21
      • 2018-05-15
      相关资源
      最近更新 更多