简体   繁体   English

带有SSR的Angular 5上的标题服务

[英]Title Service on Angular 5 with SSR

Checking Angular Universal project I found that they state the following: 检查Angular Universal项目,我发现它们陈述以下内容:

Support Title and Meta services on the server ( you can check that here ). 在服务器上支持标题和元服务您可以在此处检查 )。

But the truth is, I can't find how to implement this on the server. 但事实是,我找不到如何在服务器上实现此功能。 I checked in @angular/platform-server and couldn't fine the Title service. 我在@angular/platform-server签入,但无法对Title服务进行罚款。

Any idea how to do this? 任何想法如何做到这一点?

NOTE: I can make it work using the Title serivce in the browser. 注意:我可以使用浏览器中的标题服务来使其工作。 The problem is the server rendering 问题是服务器渲染

EDIT: setTitle works fine. 编辑:setTitle工作正常。 The issue was coming form an incorrect usage of a subscription in the server 问题来自服务器中订阅的不正确使用

在此处输入图片说明

Yeah, they are right, it's not in '@angular/platform-server' but in '@angular/platform-browser' 是的,他们是对的,它不在'@angular/platform-server'而是在'@angular/platform-browser'

Just create a service import this line 只需创建服务,即可导入此行

import { Meta, Title } from '@angular/platform-browser';

Pass in constructor Like 传递构造函数

constructor(
    private meta : Meta, 
    private title : Title
){

Now you can create meta tags and title just create a method like below and use it wherever you want 现在,您可以创建元标记和标题,只需创建如下所示的方法,然后在任何需要的地方使用它

addMetaTags(title, description) {
    //console.log(title, description);

    this.title.setTitle(title);

    this.addTag('description', description);
}
addTag(name, content) {
    //console.log(name, content);
    this.meta.addTags([{
        name: name,
        content: content
    }]);
}

You can also create og: tags for social media and other different type of metas just from about method. 您还可以仅通过about方法为社交媒体和其他不同类型的meta创建og:标签。 Ex:- 例如:-

addOgTags(title, type, url, description, image, width, height){
        if(title){
            this.addPropertyTag('og:title', title);
        }
        if(description){
            this.addPropertyTag('og:description', description);
        }
        if(url){
            this.addPropertyTag('og:url', this.domain + url);
        }
        if(image){
            this.addPropertyTag('og:image', image);
            this.addPropertyTag('og:image:url', image);
        }
        if(type){
            this.addPropertyTag('og:type', type);
        }
        if(width){
            this.addPropertyTag('og:image:width', width);
        }
        if(height){
            this.addPropertyTag('og:image:height', height);
        }
    }
    addTwitterTags(card, title, description, image){
        //console.log(card, title, description, image);
        if(card){
            this.addTag('twitter:card', card);
        }
        if(title){
            this.addTag('twitter:title', title);
        }
        if(description){
            this.addTag('twitter:description', description);
        }
        if(image){
            this.addTag('twitter:image:src', image);
        }
    }

I think you misunderstood how angular universal works. 我认为您误解了角度通用工作原理。 From the screenshot you provided, it looks like you are expecting the metadata to be set "server side" for the http://localhost:4200 server. 从您提供的屏幕快照中,您似乎期望将http:// localhost:4200服务器的元数据设置为“服务器端”。 However, this server is probably your front server, served by ng serve or other. 但是,此服务器可能是由ng serve或其他服务器提供服务的前端服务器。

To see the meta being set server side, you need to navigate to the url of the backend universal server (nodejs or asp.net core). 要查看在服务器端设置的元数据,您需要导航到后端通用服务器的URL(nodejs或asp.net core)。

In the sample repo here , it is for instance port 4000 on the server where universal is running. 此处的示例仓库中,例如,正在运行Universal的服务器上的端口4000。

Once it's working, in production you will typically have a proxy server (nginx or other) on port 80 that transfers the request to your angular universal (nodejs or asp.net core, on port 4000 or whatever, not accessible from the outside), which in turn will do the server side rendering with correct meta 工作正常后,在生产环境中,通常会在端口80上具有代理服务器(nginx或其他服务器),该请求服务器将请求传输到您的通用角度(nodejs或asp.net内核,端口4000或其他任何端口,无法从外部访问),依次使用正确的meta执行服务器端渲染

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM