// src/app/components/blog-post/blog-post.component.ts
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { CommonModule } from '@angular/common';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
import { BlogService } from '../services/blog.service';
import { FooterComponent } from './footer.component';
import { HeaderComponent } from './header.component';
@Component({
selector: 'app-blog-post',
standalone: true,
imports: [CommonModule, HeaderComponent, FooterComponent],
template: `
@if(safeContent){
{{ post?.title }}
}
`
})
export class BlogPostComponent implements OnInit {
post: any;
safeContent: SafeHtml | null = null;
constructor(
private route: ActivatedRoute,
private router: Router,
private blogService: BlogService,
private sanitizer: DomSanitizer
) {}
ngOnInit() {
const postId = this.route.snapshot.paramMap.get('id');
this.post = this.blogService.getPost(postId);
if (this.post && this.post.content) {
this.safeContent = this.sanitizer.bypassSecurityTrustHtml(this.post.content);
} else {
// Redirect to a 404 page or display an error message
this.router.navigate(['**']);
}
}
ngAfterViewInit(){
}
goBack() {
this.router.navigate(['']);
}
}