我该怎么做?
$gray-100: #f8f9fa;
$gray-600: #868e96;
$gray-800: #343a40;
$red: #dc3545;
$yellow: #ffc107;
$green: #006600; // This is the only variable I wanted to change
$cyan: #17a2b8;
$theme-colors: (
primary: $green,
secondary: $gray-600,
success: $green,
info: $cyan,
warning: $yellow,
danger: $red,
light: $gray-100,
dark: $gray-800
);
ng-bootstrap
交互。
ng new my-app --style=scss
cd my-app
npm install bootstrap@next --save
在 src/
目录下创建一个空文件 _variables.scss
,用于存放样式修改。
在 styles.scss
中添加以下内容:
@import 'variables';
@import '../node_modules/bootstrap/scss/bootstrap';
打开app.component.html文件,添加以下标记:
<button class="btn btn-primary">Test Button</button>
配置应用程序后,运行ng serve
以在开发模式下运行应用程序。在浏览器中导航到应用程序localhost:4200
。
验证引导样式按钮是否出现。为确保使用您的变量,打开_variables.scss
并添加以下内容:
$brand-primary: red;
刷新浏览器以查看字体颜色的更改。
我花了一些时间才弄清楚,希望能帮到别人,这是我创建自己的自定义样式以覆盖 ng-bootstrap
所采取的步骤:
scss
创建一个 Angular 项目来进行样式设计(不要使用 .sass!)ng add
安装 ng-bootstrap(不要使用 npm install
)。这将在我的 styles.scss
文件中添加了一行代码,如下所示:
/* Importing Bootstrap SCSS file. */
@import '~bootstrap/scss/bootstrap';
@import
上方添加属性,这意味着我可以根据需要更改主题。node_modules\bootstrap\scss\_variables.scss
中找到它们。styles.scss
示例:/* You can add global styles to this file, and also import other style files */
$primaryColor: hotpink;
$theme-colors: (primary: $primaryColor);
/* Importing Bootstrap SCSS file. */
@import '~bootstrap/scss/bootstrap';
最终成功了。我想将Bootstrap 4.3.1应用于我的Angular 7.2.15应用程序。
由于现有应用程序的默认样式模式为CSS,我使用 "从css到scss的Angular-cli" 将其转换为 SCSS(样式模式)
然后在互联网上和本页面上参考了许多地方。以下步骤对我起作用(感谢@Etherman和@Dan)。
在了解了如何将scss包含在Angular 5项目中之后(请参阅此链接:使用Angular CLI与Sass),请按照此Bootstrap教程以获得更完整的体验:https://getbootstrap.com/docs/4.0/getting-started/theming/
不要修改位于node_modules\bootstrap\scss
中的源文件,更好的方法是创建自己的.scss
样式表,并使用您想要覆盖的变量。
例如,如果您要覆盖Bootstrap默认配置中的这两个变量:
$primary;
$success;
创建一个Sass文件。在本例中,我们将在src/assets/scss/main.scss
创建我们的文件。
声明您的新变量:
$primary: #5f5ca3;
$success: #4ebc94;
最后,也是最重要的一步,导入您刚刚创建的main.scss类。 确保在导入Bootstrap样式表之前执行此操作。
在导入变量本身之前导入变量覆盖可能看起来有些违反直觉,但这是你在这里需要做的。
如果你查看那个_variables.scss文件(node_modules/bootstrap/scss/_variables.scss
),你会发现很多变量定义后面跟着!default
。
这是SCSS的方式来表达:“如果这个变量还没有被定义,那么就在这里定义它。”
但你已经自己定义了这两个变量。所以它们不会被重新定义。
因此,你的src/styles.scss
应该类似于以下内容:
/* importing KOR theme */
@import './assets/scss/main;
/* Importing Bootstrap SCSS file. */
@import '~bootstrap/scss/bootstrap';
// other style declarations and global configurations
希望有所帮助! 这里是 Bootstrap 团队的文档。