Angular universal Server Side Rendering, Meta tags

后端 未结 1 323
情歌与酒
情歌与酒 2021-01-21 08:27

I moved my site to Angular universal SSR, during my move I encountered some problems that I thought ng-universal would fix.

I want to add meta tags such as description,

1条回答
  •  温柔的废话
    2021-01-21 08:57

    To set meta tags for Angular Universal-SSR (tried only on Angular 2), you should use Renderer. To set for example title(or update it), you can do it on this way: Link for solution

    To set meta tags, you need to: 1.Import Renderer and Document

    import { Renderer } from '@angular/core';
    import { DOCUMENT } from '@angular/platform-browser';
    

    2.Add these dependencies to constructor:

    @Inject(DOCUMENT) private document: any,
    private renderer: Renderer
    

    3.Add meta tags -example

    //-- Create new meta element inside of head tag
    let elem = renderer.createElement(document.head, "meta");
    
    //-- Set meta attributes
    renderer.setElementAttribute(elem, 'name', 'description');
    renderer.setElementAttribute(elem, 'content', 'Some test decription');
    
    1. When you open "view page source", it should result like this inside of head tag:

      < meta name="description" content="Some test description" >

    0 讨论(0)
提交回复
热议问题