Angular 10/9 SEO: Adding Title and Meta Tags to Your Universal App
In this post we will give you information about Angular 10/9 SEO: Adding Title and Meta Tags to Your Universal App. Hear we will give you detail about Angular 10/9 SEO: Adding Title and Meta Tags to Your Universal AppAnd how to use it also give you demo for it if it is necessary.
In the previous tutorial, we’ve seen how to do server-side rendering of our Angular 10 application to make our app SEO friendly and increase the performance. Let’s now learn how to add meta tags to our app which is also important for SEO.
Setting Meta and Title Tags in Angular 10 for SEO Purposes
Angular provides the Meta
and Title
services that allows you to either get or set the HTML meta tags and title.
In the previous tutorial, we’ve performed the following steps:
- Step 1 – Setting up Angular 10 CLI and Initializing a Project
- Step 2 – Setting up Angular 10 Universal
- Step 3 – Building and Serving the App with Express Server
Now, let’s do one more step!
Step 4 – Setting Meta Tags and Title
Open the src/app/app.component.ts
file and start by importing the Meta and Title services as follows:
import{Component}from'@angular/core';import{Title,Meta}from'@angular/platform-browser';
Next, inject both Angular 10 services via the component constructor:
@Component({selector:'app-root',templateUrl:'./app.component.html',styleUrls:['./app.component.css']})exportclassAppComponent{title='Angular 10 Universal Example';constructor(privatetitleService:Title,privatemetaService:Meta){}}
Next, import OnInit and implement it:
import{Component,OnInit}from'@angular/core';import{Title,Meta}from'@angular/platform-browser';@Component({selector:'app-root',templateUrl:'./app.component.html',styleUrls:['./app.component.css']})exportclassAppComponentimplementsOnInit{
Next, add the ngOnInit()
method:
ngOnInit(){this.titleService.setTitle(this.title);this.metaService.addTags([{name:'keywords',content:'Angular, Universal, Example'},{name:'description',content:'Angular Universal Example'},{name:'robots',content:'index, follow'}]);}
Step 5 – Rebuild and Serve your Angular 10 App
Head back to your terminal, rebuild and serve your Angular 10 app using the following commands:
$ npm run build:ssr $ npm run serve:ssr
If you go to the http://localhost:4000/ address and inspect the source code, you should see the title changed to Angular Universal Example and the meta tags added:
Hope this code and post will helped you for implement Angular 10/9 SEO: Adding Title and Meta Tags to Your Universal App. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs