[英]Adding ld+json script tags in Angular2 component template
I have tried too many tricky ways, such as Renderer2 or ɵDomAdapter, the script tag is integrated well in the html, but when loading the url with the structured-data tool of google, the ld+json script is not rendered !我试过太多比较棘手的方法了,比如Renderer2或者ɵDomAdapter,script标签在html中集成的很好,但是用google的结构化数据工具加载url时,ld+json脚本没有渲染!
Is there a way to make google render the page after loading the component ?有没有办法让谷歌在加载组件后呈现页面?
I used this variant in Angular 9 TypeScript我在 Angular 9 TypeScript 中使用了这个变体
import { Component, OnInit } from '@angular/core';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
@Component({
selector: 'app-schema-org',
template: '<div [innerHTML]="jsonLD"></div>',
})
export class SchemaOrgComponent implements OnInit {
jsonLD: SafeHtml;
constructor(private sanitizer: DomSanitizer) { }
ngOnInit() {
const json = {
'@context': 'http://schema.org',
'@type': 'Organization',
'url': 'https://google.com',
'name': 'Google',
'contactPoint': {
'@type': 'ContactPoint',
'telephone': '+1-000-000-0000',
'contactType': 'Customer service',
},
};
// Basically telling Angular this content is safe to directly inject into the dom with no sanitization
this.jsonLD = this.getSafeHTML(json);
}
getSafeHTML(value: {}) {
const json = JSON.stringify(value, null, 2);
const html = `<script type="application/ld+json">${json}</script>`;
// Inject to inner html without Angular stripping out content
return this.sanitizer.bypassSecurityTrustHtml(html);
}
}
And then called it <app-schema-org></app-schema-org>
然后叫它
<app-schema-org></app-schema-org>
For me the example above ( https://stackoverflow.com/a/47299603/5155484 ) has no sense because it imports OnInit and implements OnChange and uses ngOnInit with a parameter for changes.对我来说,上面的例子( https://stackoverflow.com/a/47299603/5155484 )没有意义,因为它导入 OnInit 并实现 OnChange 并使用 ngOnInit 和一个参数进行更改。
So here is my working fixed example.所以这是我的工作固定示例。
There are a couple of ways to achieve this.有几种方法可以实现这一点。 The code below is the best solution I have come up with.
下面的代码是我想出的最佳解决方案。 This example will also work with Angular Universal.
这个例子也适用于 Angular Universal。
import { Component, OnInit } from '@angular/core';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
@Component({
selector: 'app-root',
template: '<div [innerHTML]="jsonLD"></div>'
})
export class JsonLdComponent implements OnChanges {
jsonLD: SafeHtml;
constructor(private sanitizer: DomSanitizer) { }
ngOnInit(changes: SimpleChanges) {
const json = {
"@context": "http://schema.org",
"@type": "Organization",
"url": "https://google.com",
"name": "Google",
"contactPoint": {
"@type": "ContactPoint",
"telephone": "+1-000-000-0000",
"contactType": "Customer service"
}
};
// Basically telling Angular this content is safe to directly inject into the dom with no sanitization
this.jsonLD = this.getSafeHTML(json);
}
getSafeHTML(value: {}) {
const json = JSON.stringify(value, null, 2);
const html = `${json}`;
// Inject to inner html without Angular stripping out content
return this.sanitizer.bypassSecurityTrustHtml(html);
}
}
I go into more detail in this blog post here https://coryrylan.com/blog/angular-seo-with-schema-and-json-ld我在这篇博文中详细介绍了https://coryrylan.com/blog/angular-seo-with-schema-and-json-ld
I also took this technique and wrapped it up into a npm package to make it more reusable.我也采用了这种技术,并将其打包到一个 npm 包中,以使其更具可重用性。 https://github.com/coryrylan/ngx-json-ld
https://github.com/coryrylan/ngx-json-ld
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.