AngularプロジェクトでBootstrapとngx-bootstrapを使う
AngularプロジェクトでBootstrapを使用する方法
Bootstrapのインストール
まず、Bootstrapをプロジェクトにインストールする必要があります。
npm install bootstrap
次に、AngularプロジェクトにBootstrapを導入する必要があります。
app.module.ts
ファイルを開き、以下のコードをインポートします。
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
NgbModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
index.html
ファイルを開き、以下のコードを追加します。
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Angular App</title>
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
</head>
Bootstrapコンポーネントを使用するには、まずNgbModule
をコンポーネントのモジュールにインポートする必要があります。
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { MyComponent } from './my.component';
@NgModule({
declarations: [
MyComponent
],
imports: [
CommonModule,
NgbModule
],
providers: [],
bootstrap: [MyComponent]
})
export class MyModule { }
次に、コンポーネントテンプレートでBootstrapコンポーネントを使用できます。
<div class="container">
<h1>Hello, world!</h1>
<p>This is a paragraph.</p>
<button type="button" class="btn btn-primary">Button</button>
</div>
BootstrapとAngularを一緒に使用するための詳細は、以下のリソースを参照してください。
補足
- 上記の手順は、Angular CLIを使用して作成されたプロジェクトを対象としています。
- Bootstrap 5を使用する場合は、
bootstrap
パッケージではなく@ng-bootstrap/ng-bootstrap
パッケージをインストールする必要があります。
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
name = 'Angular';
}
<h1>{{name}}</h1>
<p>This is a paragraph.</p>
<button type="button" class="btn btn-primary">Button</button>
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Angular App</title>
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
</head>
<body>
<app-root></app-root>
</body>
</html>
このコードを実行すると、ブラウザに以下のような画面が表示されます。
このコードは、Bootstrapの基本的なコンポーネントであるボタンを使用しています。Bootstrapには他にもさまざまなコンポーネントがあるので、それらを使用してさまざまなUIを作成することができます。
AngularプロジェクトでBootstrapを使用するその他の方法
ngx-bootstrapは、AngularプロジェクトでBootstrapを使用するためのオープンソースライブラリです。ngx-bootstrapを使用すると、Bootstrapコンポーネントを簡単に使用できます。
npm install ngx-bootstrap
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
NgbModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Angular App</title>
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
</head>
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { MyComponent } from './my.component';
@NgModule({
declarations: [
MyComponent
],
imports: [
CommonModule,
NgbModule
],
providers: [],
bootstrap: [MyComponent]
})
export class MyModule { }
<div class="container">
<h1>Hello, world!</h1>
<p>This is a paragraph.</p>
<button type="button" class="btn btn-primary">Button</button>
</div>
CDNを使用してBootstrapをプロジェクトに読み込むこともできます。
CDNの追加
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Angular App</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
</head>
Bootstrapコンポーネントを使用するには、BootstrapのJavaScriptファイルをプロジェクトに読み込む必要があります。
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<div class="container">
<h1>Hello, world!</h1>
<p>This is a paragraph.</p>
<button type="button" class="btn btn-primary">Button</button>
</div>
Bootstrapのソースコードをプロジェクトにダウンロードして使用することもできます。
Bootstrapの公式サイトからBootstrapのソースコードをダウンロードします。
ダウンロードしたBootstrapのソースコードをプロジェクトに追加します。
**3.3. Bootstrapコンポーネントの使用
angular twitter-bootstrap typescript