Angular と Firestore を使用してブログを作成する方法について、完全かつ包括的なガイドをお届けします。このガイドでは、Angular フレームワークを使ってブログアプリケーションを構築し、Firestore データベースを活用して投稿を管理・表示する方法について詳しく説明します。以下の手順に従って、実際に動作するブログアプリケーションを作成できます。
1. プロジェクトのセットアップ
1.1 Angular のインストール
まず、Angular CLI をインストールします。以下のコマンドをターミナルで実行してください。

bashnpm install -g @angular/cli
その後、新しい Angular プロジェクトを作成します。
bashng new blog-app
cd blog-app
1.2 Firebase と Firestore の設定
Firebase を使用するためには、まず Firebase コンソールから新しいプロジェクトを作成します。
- Firebase コンソールにアクセスし、新しいプロジェクトを作成します。
- Firestore を有効にします。
- Firebase の設定情報を取得します。これには、API キー、認証ドメイン、データベース URL などが含まれます。
次に、Angular アプリケーションに Firebase SDK をインストールします。
bashnpm install firebase @angular/fire
1.3 Firebase 設定を Angular に追加
Firebase の設定情報を Angular プロジェクトに追加します。src/environments/environment.ts
ファイルに次のように記述します。
typescriptexport const environment = {
production: false,
firebaseConfig: {
apiKey: "あなたのAPIキー",
authDomain: "あなたのプロジェクトID.firebaseapp.com",
databaseURL: "https://あなたのプロジェクトID.firebaseio.com",
projectId: "あなたのプロジェクトID",
storageBucket: "あなたのプロジェクトID.appspot.com",
messagingSenderId: "あなたの送信者ID",
appId: "あなたのアプリID"
}
};
2. Firestore サービスの作成
Firestore とのやり取りを行うサービスを作成します。このサービスは、データの追加、取得、削除を担当します。
bashng generate service services/firestore
firestore.service.ts
ファイルを以下のように編集します。
typescriptimport { Injectable } from '@angular/core';
import { AngularFirestore } from '@angular/fire/compat/firestore';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class FirestoreService {
constructor(private firestore: AngularFirestore) { }
// すべてのブログ投稿を取得
getPosts(): Observable<any[]> {
return this.firestore.collection('posts').valueChanges();
}
// 新しい投稿を追加
addPost(post: any): Promise<void> {
const id = this.firestore.createId();
return this.firestore.collection('posts').doc(id).set(post);
}
}
3. 投稿の作成画面の作成
次に、ブログ投稿を作成するためのフォームを作成します。app.component.html
ファイルに以下のようにフォームを追加します。
html<div>
<h2>新しい投稿を作成h2>
<form (ngSubmit)="onSubmit()">
<label for="title">タイトルlabel>
<input type="text" id="title" [(ngModel)]="newPost.title" name="title" required />
<label for="content">内容label>
<textarea id="content" [(ngModel)]="newPost.content" name="content" required>textarea>
<button type="submit">投稿button>
form>
div>
app.component.ts
ファイルに次のようにロジックを追加します。
typescriptimport { Component } from '@angular/core';
import { FirestoreService } from './services/firestore.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
newPost = { title: '', content: '' };
constructor(private firestoreService: FirestoreService) {}
onSubmit() {
if (this.newPost.title && this.newPost.content) {
this.firestoreService.addPost(this.newPost).then(() => {
this.newPost = { title: '', content: '' };
});
}
}
}
4. 投稿の表示画面の作成
次に、投稿されたブログ記事を表示する画面を作成します。app.component.html
に投稿リストを表示する部分を追加します。
html<div>
<h2>ブログ投稿h2>
<ul>
<li *ngFor="let post of posts">
<h3>{{ post.title }}h3>
<p>{{ post.content }}p>
li>
ul>
div>
app.component.ts
ファイルに以下のコードを追加して、Firestore から投稿を取得し、表示するようにします。
typescriptimport { Component, OnInit } from '@angular/core';
import { FirestoreService } from './services/firestore.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
posts: any[] = [];
newPost = { title: '', content: '' };
constructor(private firestoreService: FirestoreService) {}
ngOnInit(): void {
this.firestoreService.getPosts().subscribe(posts => {
this.posts = posts;
});
}
onSubmit() {
if (this.newPost.title && this.newPost.content) {
this.firestoreService.addPost(this.newPost).then(() => {
this.newPost = { title: '', content: '' };
});
}
}
}
5. Angular アプリケーションの実行
最後に、Angular アプリケーションを実行して、ブログの作成と表示機能が正常に動作するか確認します。
bashng serve
ブラウザで http://localhost:4200
にアクセスし、新しい投稿を作成し、投稿が正しく表示されることを確認します。
結論
以上の手順で、Angular と Firestore を使用してブログアプリケーションを作成する方法が完了しました。このブログアプリケーションは、ユーザーがブログ投稿を追加し、それらを表示する基本的な機能を提供します。これをさらに拡張して、投稿の編集や削除、ユーザー認証機能などを追加することができます。