【发布时间】:2018-03-28 20:58:01
【问题描述】:
我正在尝试添加 facebook 将拾取的 og 标签,从我在网上找到的内容来看,听起来 angular universal 现在已合并到 angular 中,并且应该能够通过使用平台浏览器库来做到这一点。
我开始了一个全新的 .net 核心项目,支持角度。它创建了 3 个角度组件,我希望我可以对其进行测试以尝试设置 og 标签 - counter.component.ts、fetchdata.component.ts 和 home.component.ts。下面是我在 counter.component.ts 构造函数中的示例。
constructor(private meta: Meta) {
this.meta.addTags([
{ property: 'og:url', content: 'https://www.oursite.com/counter' },
{ property: 'og:title', content: 'Counter Page' },
{ property: 'og:description', content: 'Counter page description' },
{ property: 'og:image', content: 'https://www.oursite.com/images/angular/counter-page.png' }
], false);
}
当我运行它并查看源代码时,它实际上添加了 og 标记,但是它们在 app 标记内它自己的 html 结构中呈现。
<html>
<body>
<app>
<html>
<head>
<meta property="og:title" content="Counter Page">
我为 boot.server.ts 文件找到了一些代码,它删除了只留下元数据的额外标签,但因为它们不在实际的头标签中,facebook 的调试器仍然无法识别它们。
resolve({
html: state.renderToString()
.replace('<html><head>', '')
.replace('</head><body>', '')
.replace(/<\s*app.*?>/, '')
.replace('</app></body></html>', '')
});
我读过的所有内容都让人觉得它应该是这样工作的,而且它对 SEO/社交媒体很友好。但显然不是。我不确定这是它的预期工作方式还是 .net 核心模板设置不正确?
【问题讨论】: