Angular 6 Library using bootstrap

后端 未结 6 2116
遇见更好的自我
遇见更好的自我 2021-02-19 13:14

I am creating an angular library in an angular project using angular 6 library feature https://github.com/angular/angular-cli/wiki/stories-create-library

I am creating f

相关标签:
6条回答
  • 2021-02-19 13:24

    When you deliver your library you should add bootstrap as a peerDependency to package.json:

     "peerDependencies": {
        "ngx-bootstrap": ">=<MIN-VERSION> <MAX-VERSION",
      }
    

    Due to peerDependencies when someone installs your library ngx-bootstrap will get installed automatically, if it's not present. When the wrong version is installed the user of your library gets a warning.

    Here some more info: http://npm.github.io/using-pkgs-docs/package-json/types/peerdependencies.html

    0 讨论(0)
  • 2021-02-19 13:30

    I don't know if it answers your question, but firstly you have to add bootstrap to your library peer dependencies, after that, in all your projects which use that library you have to include bootstrap styles to global styles in angular.json.


    After that you can use bootstrap classes from templates in your library and you can be sure that styles would be applied. Also, if you want to use mixins or variables from bootstrap, you can just import needed scss bootstrap files into your components' scss files in library, like that:

    @import "~bootstrap/scss/bootstrap-grid";
    
    @include media-breakpoint-up(sm) {
        .some-class {
          display: block;
        }
    }
    

    During build all these imported scss files would be compiled and built into your library output files.


    There are other ways to include styles to library, but all of them require interception/extension of library build process.

    0 讨论(0)
  • 2021-02-19 13:33

    You can simply install normal bootstrap with the command below

    npm i bootstrap
    

    Then since you are using Angular 6, you will need an angular.json file anf the @angular-devkit/build-angular, not angular-cli.json. Install the devkit with the command below

    npm i @angular-devkit/build-angular --save-dev
    

    and add this line of code in the "architect" section under "build" and into "options" you need to place the style references. I have given an example below.

    "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "public",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "preserveSymlinks": true,
            "styles": [
              "node_modules/bootstrap/dist/css/bootstrap.min.css",
              "node_modules/bootstrap/dist/css/bootstrap-theme.min.css"],
          },
          "configurations": {
            "production": {
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": false,
              "assets": [
                "src/favicon.ico",
                "src/assets"
              ],
            },
            "local": {
              "assets": [
                "src/favicon.ico"
              ]
            }
          }
        }
    

    I recommend referring to the docs on the specifics around the devkit and the angular.json configuration

    0 讨论(0)
  • 2021-02-19 13:39

    If you are using ng-packagr, you can use the below process that I followed.

    I added the styles in styleIncludedPaths of ng-package.json as shown below.

    "lib": {
        "entryFile": "src/public_api.ts",
        "cssUrl": "inline",
        "styleIncludePaths": [
          "src/assets/styles",
          "../../node_modules/bourbon/app/assets/stylesheets",
          "../../node_modules/bourbon-neat/app/assets/stylesheets",
          "../../node_modules/bootstrap/dist/css"
        ]
      }
    
    0 讨论(0)
  • 2021-02-19 13:43

    I think many of the answers here missed that the question was NOT about how to add bootstrap to an angular app. It's specifically about how to add bootstrap to a custom angular library. Not sure if you already found the solution, but this is what worked for me.

    1. Add bootstrap in the peerDependencies section of project/your-library/package.json, e.g.
    {
        "name": "your-library",
        "version": "0.0.1",
        "peerDependencies": {
            "@angular/common": "^6.0.0-rc.0 || ^6.0.0",
            "@angular/core": "^6.0.0-rc.0 || ^6.0.0",
            "bootstrap": "^4.3.1"
        }
    }
    

    This will install bootstrap when your library is used as a dependency in another project.

    1. Create a .scss file (e.g your-component.scss) at the same level as your component in the library and have this line:
    @import "node_modules/bootstrap/scss/bootstrap"
    
    1. In your component, specify the .scss file you created in step 2 as styleUrls, e.g.
    @Component({
        selector: 'your-lib',
        templateUrl: './your-component.html',
        styleUrls: ['./your-component.scss']
    })
    export class YourComponent {
    
        ....
    
    }
    
    1. Add bootstrap in the devDependencies section of the top level package.json of the project containing your library. This will allow you to use bootstrap while you are developing the library

    2. npm install in the project root

    3. ng build your-library in the project root

    0 讨论(0)
  • 2021-02-19 13:43

    1) you can directly install using

    npm install bootstrap
    

    and than in your angular cli you can inlude it using

    "styles": [
              "node_modules/bootstrap/dist/css/bootstrap.min.css",
            ],
    

    2) you can also use ngx-bootstrap, it will help you in many input components

    npm install ngx-bootstrap
    
    0 讨论(0)
提交回复
热议问题