【问题标题】:I know Bootstrap, can I use MD Bootstrap and or Angular Material components in the same project?我知道 Bootstrap,我可以在同一个项目中使用 MD Bootstrap 和/或 Angular Material 组件吗?
【发布时间】:2019-11-10 00:56:16
【问题描述】:

我对 Bootstrap 已经有一段时间了。我刚刚学习 Angular,我现在正在研究 MD BootstrapAngular Material

但我有点困惑。这两个框架,特别是它们的组件,可以作为 Bootstrap 的补充吗?

我知道它们可以用来代替 Bootstrap,但是如果我喜欢 Bootstrap 并且只想使用 Angular Material 中的 2 个组件呢?

例如。侧边导航

Bootstrap 没有本机 sidenav,所以我正在尝试查看 Angular Meterials sidenav 是否可以工作...

【问题讨论】:

  • 你可以,但为什么呢?
  • 因为我想要一个好的sidenav,但我认为Bootstrap 没有原生的。但是 Angular Material 可以。如果我想要其他不在 Bootstrap 中但属于 MD Bootstrap 的组件怎么办?而且看起来 MD Bootstrap 在他们的网站上有很多我可以使用的免费模板。比如登陆页面、管理页面等
  • 如果您可以编写自己的 Css,我建议您使用 Angular 材料。否则使用引导程序。您可以同时添加两者,它仍然有效,但您会添加不必要的依赖项。
  • 好吧,那是我关心的问题!如果它使用了不必要的依赖,会对整体性能产生多大影响?

标签: angular twitter-bootstrap angular-material mdbootstrap


【解决方案1】:

我已经尝试了一些使用 bootstrap 和材料的东西。

你们可以一起建造一些东西。

看看这篇博文:https://www.amadousall.com/the-good-parts-of-bootstrap-4-you-are-missing-in-your-angular-material-projects/

解释了如何使用 scss 构建好东西。

我喜欢将所有引导实用程序类与材料结合在一起的解决方案。

现在的问题是为什么有人应该这样做?

因为 bootstrap 有很多非常好的助手,基本样式,尤其是最好的 reboot.scss,从 normalize.css 扩展而来。还有一些其他的好东西,比如 flex-box 助手,也许还有纯 css 网格系统。

使用 SCSS,您可以选择只导入您想要的部分,而不是导入所有内容,这是一件很酷的事情。

这是我的 SCSS 工作基础,只需从 bootstrap 扩展您想要的部分:

_variables.scss

$link-color: #3f51b5;
$link-hover-color: currentColor;
$link-hover-decoration: none;

_reset.scss


* {
  &:active,
  :focus {
    outline: none !important;  // 1
  }
}

label {
  margin-bottom: 0; // 2
}


a:not(.mat-button):not(.mat-raised-button):not(.mat-fab):not(.mat-mini-fab):not([mat-list-item]) {
  color: #3f51b5; // 3
}

main.scss

@import 'variables';
@import '~bootstrap/scss/functions';
@import '~bootstrap/scss/variables';
@import '~bootstrap/scss/mixins';
@import '~bootstrap/scss/reboot';
@import '~bootstrap/scss/utilities';
@import 'reset';

@import '~@angular/material/theming';
// Plus imports for other components in your app.

// Include the common styles for Angular Material. We include this here so that you only
// have to load a single css file for Angular Material in your app.
// **Be sure that you only ever include this mixin once!**
@include mat-core();

// Define the default theme (same as the example above).
$candy-app-primary: mat-palette($mat-indigo);
$candy-app-accent:  mat-palette($mat-pink, A200, A100, A400);
$candy-app-theme:   mat-light-theme($candy-app-primary, $candy-app-accent);

// Include the default theme styles.
@include angular-material-theme($candy-app-theme);

// Define an alternate dark theme.
$dark-primary: mat-palette($mat-blue-grey);
$dark-accent:  mat-palette($mat-amber, A200, A100, A400);
$dark-warn:    mat-palette($mat-deep-orange);
$dark-theme:   mat-dark-theme($dark-primary, $dark-accent, $dark-warn);

// Include the alternative theme styles inside of a block with a CSS class. You can make this
// CSS class whatever you want. In this example, any component inside of an element with
// `.unicorn-dark-theme` will be affected by this alternate dark theme instead of the default theme.
.unicorn-dark-theme {
  @include angular-material-theme($dark-theme);
}


// Your styles

【讨论】:

    【解决方案2】:

    MD BootstrapAngular Material 是两个不同的包,试图实现相同的功能,提供现成的组件。

    在当前的开发场景中,没有明确需要像过去那样使用 Bootstrap。现在,Bootstrap 大部分时间都用于布局结构,如果您只想将它​​用于布局(bootstrap-grid.min.css),您可以获得一个精简版的 bootstrap。此外,Bootstrap 尝试支持大多数浏览器,包括 IE10 和 11。

    另一方面,Angular Material 专注于组件及其行为。并且主要适用于所有现代浏览器(Chrome,主要是 Chrome),并且在布局方面几乎没有提供任何内容。它确实支持主题、自定义样式等。

    您可以在您的项目中使用这两种方法,它们都很稳定并且有很多社区支持。但他们在自己的方式上是非常不同的。最好只使用其中一个。

    但是如果你真的需要一些组件,你仍然可以从 Angular 材质中导入单个模块,无需导入完整的材质模块并使用它,这样可以确保最小的包大小。所以我建议你使用 MD-bootstrap 作为主要组件库,并且导入可能是来自 Angular 材料的 2 或 3 个组件。您必须为 Angular 材质组件添加自定义样式。

    【讨论】:

    • 所以你是说我应该使用“bootstrap-grid.min.css”作为网格和布局,然后 md bootstrap 作为主要?是什么让您认为 md bootstrap 比常规 bootstrap 更好?
    • 没有。我的意思是,如果您仅将引导程序用于布局,那么请选择bootstrap-grid.min.cssmd-bootstrap 只提供组件和引导程序。
    • 所以如果我使用的是 Bootstrap 4 并且我想使用一个 MD 引导组件,我可以只导入单个 MD Bootstrap 组件并使用它,而无需导入整个库?
    【解决方案3】:

    是的,您可以使用 Angular Material 作为引导程序的补充。

    我个人使用过Angular Material Table

    在我的许多项目中使用动态数据加载。

    我还使用了角度材料的自动完成组件。

    最好的部分是这些组件很容易配置

    【讨论】:

    • 同时使用 Bootstrap 和 Material 会影响性能吗?我认为组件的整个想法就是只为每个组件使用代码,仅此而已?
    • 嗯,这取决于你如何导入库。您只能从 Angular 材料中包含所需的组件。使用起来非常轻便。
    • 好的,这就是我需要做的。看,我只想在 Angular Material 中使用侧边栏。我的应用程序的其余部分使用 Bootstrap 4...和其他一些 ngx-bootstrap 组件,
    猜你喜欢
    • 2020-09-25
    • 2019-02-03
    • 2021-11-29
    • 2019-10-09
    • 2021-12-15
    • 2018-07-29
    • 2018-01-08
    • 2012-02-21
    • 1970-01-01
    相关资源
    最近更新 更多