Create new a folder src/app/models. Create a new file 📝src/app/models/post.ts and paste the following code
export class Post {
postId: string;
title: string;
content: string;
createdDate: any;
constructor() {
this.content = '';
}
}
Create the blog service
We will create a service to handle our database operations. Create a new service by making a right-click on the folder 📁servicesand navigating to 'Angular Generator','Component' and provide the name ‘blog’.
Open the 📝src/app/services/blog.service.ts file and add the following import definitions.
import { AngularFirestore } from '@angular/fire/firestore';
import { Post } from '../models/post';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';
We will create a new component for adding and editing the blog. Let’s make a right-click on the folder 📁components. Navigate to 'Angular Generator', select 'Component' and provide the name ‘blog-editor’.
Add a route to the addpost page
Add the route for this component in 📝app.module.ts as shown below:
Open 📝src/app/components/blog-editor/blog-editor.component.ts and add the import definitions:
import * as ClassicEditor from '@ckeditor/ckeditor5-build-classic';
import { Post } from 'src/app/models/post';
import { DatePipe } from '@angular/common';
import { BlogService } from 'src/app/services/blog.service';
import { Router, ActivatedRoute } from '@angular/router';
public Editor = ClassicEditor;
ckeConfig: any;
postData = new Post();
formTitle = 'Add';
postId = '';
We will now implement the feature of adding a new blog to our application. Open 📝src/app/components/blog-editor.component.ts and inject the following service definitions in the constructor.
This method will be invoked on click of Save button. We will add the following code definition for Cancel button.
cancel() {
this._router.navigate(['/']);
}
Add buttons in Nav bar
We will add the navigation button to blog editor and home page in the nav bar. Add the following code inside the <mat-toolbar> in the file 📝src/app/components/nav-bar/nav-bar.component.html:
<mat-toolbar class="nav-bar mat-elevation-z2">
<button mat-button [routerLink]='[""]'> My blog </button>
<button mat-button [routerLinkActive]='["link-active"]' [routerLink]='["/addpost"]'>
Add Post
</button>
</mat-toolbar>
Add BlogEditorComponent styles
We will add styling for blog editor in 📝styles.scss file with the following code:
Open the browser and click on “AddPost” button on the nav-bar. You will be navigated to the blog editor page. Add a new blog and click on save button to save the blog in thee database. Open the firebase console, navigate to your project overview page and click on “Database” link in the menu on the left. Here you can see the record for your newly added blog.
Now we will add the method to create a new post. The method to add a new blog post is shown at . Put this method in blog.service.ts file.
We will use for adding and editing our blog post. CKEditor is a Smart WYSIWYG(What you see is what you get) editor which provides us with great editing capabilities.
We will also initialize some properties as shown at
We will create a method to define the configuration for the blog editor. You can get the method definition from
Open src/app/components/blog-editor/blog-editor.component.html and put the code as shown at