首页 > 解决方案 > 如何从 npm 包中安装特定文件,例如仅从 bootstrap bootstrap.min.css?

问题描述

我正在使用 npm 在 react 中使用从 bootstrap 包安装的 bootstrap.min.css ,但这让我很好奇,有什么方法可以让我们从 bootstrap 包而不是完整包中安装特定文件。此外,如果我们在我的情况下使用特定文件,则仅 bootstrap.min.css 而不是 bootstrap 包中的其他文件。是否会影响应用程序的性能。

标签: node.jsreactjstwitter-bootstrapnpm

解决方案


如果你知道 Bootstrap 组件的哪一部分,你就可以。

CSS

在项目的 scss 文件中导入bootstrap/scss/您需要的 scss 文件。您必须对样式表使用 scss 文件格式。

// Required
@import "../node_modules/bootstrap/scss/functions";
@import "../node_modules/bootstrap/scss/variables";
@import "../node_modules/bootstrap/scss/mixins";
// Optional
@import "../node_modules/bootstrap/scss/reboot";
...

参考:https ://getbootstrap.com/docs/4.3/getting-started/theming/#importing

JS

将 .js 文件导入bootstrap/js/dist/.

import 'bootstrap/js/dist/util';
import 'bootstrap/js/dist/alert';
...

参考:https ://getbootstrap.com/docs/4.3/getting-started/webpack/#importing-javascript

并且使用特定组件使您具有紧凑的构建尺寸和更快的加载速度。


推荐阅读