Angular Material を使って Bootstrap の代わりに Angular プロジェクトで UI コンポーネントを表示する
Angular CLI プロジェクトに Bootstrap を追加する方法
手順
- 必要なライブラリのインストール
以下のコマンドを実行して、必要なライブラリをインストールします。
npm install --save ng2-bootstrap bootstrap jquery
angular-cli.json
ファイルを開き、以下の内容を追加します。
"styles": [
"styles.css",
"../node_modules/bootstrap/dist/css/bootstrap.min.css"
],
"scripts": [
"../node_modules/jquery/dist/jquery.min.js",
"../node_modules/bootstrap/dist/js/bootstrap.min.js"
]
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { Ng2BootstrapModule } from 'ng2-bootstrap';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
Ng2BootstrapModule.forRoot()
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
コードの使用
これで、Bootstrap のコンポーネントやディレクティブをプロジェクトで使用できます。
例
<button type="button" class="btn btn-primary">ボタン</button>
- CDN を使用して Bootstrap を読み込む
- Bootstrap の Sass ファイルをプロジェクトに直接組み込む
これらの方法は、上記の手順よりも複雑な場合がありますので、詳細は各方法のドキュメントを参照してください。
補足
ng2-bootstrap
ライブラリは、Bootstrap 4 を Angular で使用するためのラッパーライブラリです。- Bootstrap 5 を使用したい場合は、
ng-bootstrap
ライブラリを使用する必要があります。
注意
- Bootstrap をプロジェクトに追加すると、プロジェクトのサイズが大きくなります。
- Bootstrap は、JavaScript フレームワークではありません。
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Angular CLI プロジェクト</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<app-root></app-root>
</body>
</html>
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Angular CLI プロジェクト';
}
<h1>{{title}}</h1>
<button type="button" class="btn btn-primary">ボタン</button>
styles.css
/*
Bootstrap のスタイルシートをインポート
*/
@import '~bootstrap/dist/css/bootstrap.min.css';
package.json
{
"name": "angular-cli-project",
"version": "0.0.0",
"description": "Angular CLI プロジェクト",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "^14.2.0",
"@angular/common": "^14.2.0",
"@angular/compiler": "^14.2.0",
"@angular/core": "^14.2.0",
"@angular/forms": "^14.2.0",
"@angular/platform-browser": "^14.2.0",
"@angular/platform-browser-dynamic": "^14.2.0",
"@angular/router": "^14.2.0",
"bootstrap": "^5.1.3",
"jquery": "^3.6.0",
"ng2-bootstrap": "^5.2.0",
"rxjs": "~7.5.0",
"tslib": "^2.4.0",
"zone.js": "~0.11.6"
},
"devDependencies": {
"@angular/cli": "^14.2.0",
"@angular/compiler-cli": "^14.2.0",
"@angular-devkit/build-angular": "^14.2.0",
"@angular-devkit/build-ng-packagr": "^14.2.0",
"@angular-devkit/build-webpack": "^14.2.0",
"@angular-devkit/core": "^14.2.0",
"@angular-devkit/schematics": "^14.2.0",
"@types/jasmine": "~4.0.0",
"@types/node": "^18.0.0",
"jasmine-core": "~4.1.0",
"jasmine-spec-reporter": "~7.0.0",
"karma": "~7.1.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage": "~2.2.0",
"karma-jasmine": "~5.0.0",
"karma-jasmine-html-reporter": "~1.7.0",
"protractor": "~7.0.0",
"ts-node": "~10.8.0",
"tslint": "~6.1.0",
"typescript": "~4.8.0"
}
}
上記コードを実行すると、ブラウザにボタンが表示されます。
その他のサンプル
- [ng2-bootstrap 公式ドキュメント](https://ng2-bootstrap.github.
Angular CLI プロジェクトに Bootstrap を追加するその他の方法
Bootstrap CDN を使用して、Bootstrap の CSS と JavaScript ファイルをプロジェクトに直接読み込むことができます。
- Bootstrap CDN の URL を取得します。
index.html
ファイルに以下のコードを追加します。
<head>
...
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
</head>
Bootstrap の Sass ファイルをプロジェクトに直接組み込み、プロジェクトの Sass/SCSS ファイルで Bootstrap をカスタマイズすることができます。
- プロジェクトの
styles
フォルダに Sass ファイルをコピーします。 - プロジェクトの Sass/SCSS ファイルで Bootstrap をインポートします。
@import '~bootstrap/scss/bootstrap';
Angular Material は、Angular プロジェクトで使用できる UI コンポーネントライブラリです。Angular Material は Bootstrap に似たコンポーネントを提供しており、Bootstrap の代わりに使用することができます。
- Angular Material をプロジェクトにインストールします。
npm install --save @angular/material
app.module.ts
ファイルにMatModule
をインポートします。
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { MatModule } from '@angular/material';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
MatModule.forRoot()
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
- コードで Angular Material コンポーネントを使用します。
<button mat-raised-button>ボタン</button>
Angular CLI プロジェクトに Bootstrap を追加するにはいくつかの方法があります。それぞれの方法にはメリットとデメリットがありますので、プロジェクトの要件に合わせて最適な方法を選択してください。
angular twitter-bootstrap angular-cli