{"id":445,"date":"2020-09-22T13:23:41","date_gmt":"2020-09-22T11:23:41","guid":{"rendered":"http:\/\/www.myblog.nguenkam.com\/?p=445"},"modified":"2022-06-08T17:59:52","modified_gmt":"2022-06-08T15:59:52","slug":"how-to-add-bootstrap-to-an-angular-cli-project","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2020\/09\/22\/how-to-add-bootstrap-to-an-angular-cli-project\/","title":{"rendered":"How to add Bootstrap to an Angular CLI project"},"content":{"rendered":"\n<p>I used to struggle with this case, long time ago, how to setup an Angular project generated with Angular CLI with Bootstrap. So let\u2019s see the step by step in the sections below.<\/p>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-cyan-blue-color\">1: Creating an Angular project with Angular CLI<\/span><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>ng new angular-bootstrap-example<\/code><\/pre>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-cyan-blue-color\">2: Installing Bootstrap from NPM<\/span><\/h4>\n\n\n\n<p>For Bootstrap 3:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install bootstrap@3.3.7<\/code><\/pre>\n\n\n\n<p>For Bootstrap 4:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install bootstrap<\/code><\/pre>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-red-color\">2.1: Alternative: Local Bootstrap CSS<\/span><\/h4>\n\n\n\n<p>As an alternative, you can also download the Bootstrap CSS and add it locally to your project. let us donwload Bootstrap&nbsp;<a href=\"https:\/\/getbootstrap.com\/\">from the website<\/a>&nbsp;and create a folder&nbsp;<code>styles<\/code>&nbsp;(same level as&nbsp;<code>styles.css<\/code>):<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" src=\"https:\/\/loiane.com\/assets\/images\/2017\/angular-cli-bootstrap-01.png\" alt=\"\" width=\"224\" height=\"220\"\/><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>&nbsp;Don\u2019t place your local CSS files under&nbsp;<code>assets<\/code>&nbsp;folder. When we do the production build with Angular CLI, the CSS files declared in the&nbsp;<code>angular.json<\/code>&nbsp;will be minified and all styles will be bundled into a single styles.css. The assets folder is copied to the dist folder during the build process (the CSS code will be duplicated). Only place your local CSS files under&nbsp;<code>assets<\/code>&nbsp;in case you are importing them directly in the&nbsp;<code>index.html<\/code>.<\/p><\/blockquote>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-cyan-blue-color\">3: Importing the CSS<\/span><\/h4>\n\n\n\n<p>We have two options to import the CSS from Bootstrap that was installed from NPM:<\/p>\n\n\n\n<p>1: Configure&nbsp;<code>angular.json<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"styles\": &#91;\n  \"node_modules\/bootstrap\/dist\/css\/bootstrap.min.css\",\n  \"styles.scss\"\n]<\/code><\/pre>\n\n\n\n<p>2: Import directly in&nbsp;<code>src\/style.css<\/code>&nbsp;or&nbsp;<code>src\/style.scss<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@import '~bootstrap\/dist\/css\/bootstrap.min.css';<\/code><\/pre>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-red-color\">3.1 Alternative: Local Bootstrap CSS<\/span><\/h4>\n\n\n\n<p>If you added the Bootstrap CSS file locally, just import it in&nbsp;<code>angular.json<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"styles\": &#91;\n  \"styles\/bootstrap-3.3.7-dist\/css\/bootstrap.min.css\",\n  \"styles.scss\"\n],<\/code><\/pre>\n\n\n\n<p>or&nbsp; import it in <code>src\/style.css<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@import '.\/styles\/bootstrap-3.3.7-dist\/css\/bootstrap.min.css';<\/code><\/pre>\n\n\n\n<p>Yet, we are able to start using the Bootstrap CSS classes in our project.<\/p>\n\n\n\n<p>In case we don\u2019t need to use Bootstrap JavaScript components (that require JQuery), this is all the setup we need. <span class=\"has-inline-color has-vivid-cyan-blue-color\">But if we need to use modals, accordion, datepicker, tooltips or any other component, how can we use these components without installing jQuery?<\/span><\/p>\n\n\n\n<p>There is an Angular wrapper library for Bootstrap called&nbsp;<a href=\"http:\/\/valor-software.com\/ngx-bootstrap\">ngx-bootstrap<\/a>&nbsp;that we can also install from NPM:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install ngx-bootstrap --save<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><code>ng2-bootstrap<\/code>&nbsp;and&nbsp;<code>ngx-bootstrap<\/code>&nbsp;are the same package. ng2-bootstrap was renamed to ngx-bootstrap after&nbsp;<code>#itsJustAngular<\/code>.<\/p><\/blockquote>\n\n\n\n<h4><span class=\"has-inline-color has-black-color\">4: Bootstrap JavaScript Components with ngx-bootstrap <\/span><\/h4>\n\n\n\n<h4><span class=\"has-inline-color has-black-color\">4.1 : Adding the required Bootstrap modules in app.module.ts<\/span><\/h4>\n\n\n\n<p>Go through the&nbsp;<code>ngx-bootstrap<\/code>&nbsp;and add the modules needed in your&nbsp;<code>app.module.ts<\/code>. For example, suppose we want to use the Dropdown, Tooltip and Modal components:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mport { BsDropdownModule } from 'ngx-bootstrap\/dropdown';\nimport { TooltipModule } from 'ngx-bootstrap\/tooltip';\nimport { ModalModule } from 'ngx-bootstrap\/modal';\n\n@NgModule({\n  imports: &#91;\n    BrowserModule,\n    BsDropdownModule.forRoot(),\n    TooltipModule.forRoot(),\n    ModalModule.forRoot()\n  ],\n  \/\/ ...\n})\nexport class AppBootstrapModule {}<\/code><\/pre>\n\n\n\n<p>Because we call the&nbsp;<code>.forRoot()<\/code>&nbsp;method for each module (due the ngx-bootstrap module providers), the functionalities will be available in all components and modules of your project (global scope).<\/p>\n\n\n\n<p>As an alternative, if you would like to organize the&nbsp;<code>ngx-bootstrap<\/code>&nbsp;in a different module (just for organization purposes in case you need to import many bs modules and don\u2019t want to clutter your app.module), you can create a module&nbsp;<code>app-bootstrap.module.ts<\/code>, import the Bootstrap modules (using&nbsp;<code>forRoot()<\/code>) and also declare them in the exports section (so they become available to other modules as well).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { NgModule } from '@angular\/core';\nimport { CommonModule } from '@angular\/common';\n\nimport { BsDropdownModule } from 'ngx-bootstrap\/dropdown';\nimport { TooltipModule } from 'ngx-bootstrap\/tooltip';\nimport { ModalModule } from 'ngx-bootstrap\/modal';\n\n@NgModule({\n  imports: &#91;\n    CommonModule,\n    BsDropdownModule.forRoot(),\n    TooltipModule.forRoot(),\n    ModalModule.forRoot()\n  ],\n  exports: &#91;BsDropdownModule, TooltipModule, ModalModule]\n})\nexport class AppBootstrapModule {}<\/code><\/pre>\n\n\n\n<p>At last, don\u2019t forget to import your bootstrap module in you&nbsp;<code>app.module.ts<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { AppBootstrapModule } from '.\/app-bootstrap\/app-bootstrap.module';\n\n@NgModule({\n  imports: &#91;BrowserModule, AppBootstrapModule],\n  \/\/ ...\n})\nexport class AppModule {}<\/code><\/pre>\n\n\n\n<p>Now, We have an Angular project using Bootstrap and did not need to import JQuery to have the same behavior!<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h5>Reference<\/h5>\n\n\n\n<p><a href=\"https:\/\/loiane.com\/\">https:\/\/loiane.com\/<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I used to struggle with this case, long time ago, how to setup an Angular project generated with Angular CLI with Bootstrap. So let\u2019s see the step by step in the sections below. 1: Creating an Angular project with Angular CLI 2: Installing Bootstrap from NPM For Bootstrap 3: For Bootstrap 4: 2.1: Alternative: Local [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1965,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[37,108,89],"tags":[40,109],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/445"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=445"}],"version-history":[{"count":5,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/445\/revisions"}],"predecessor-version":[{"id":459,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/445\/revisions\/459"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1965"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}