英文:
Dynamically added meta tags are not showing images, description and title when I share a link on WhatsApp. (Angular 2)
问题
我可以动态设置元标签,当我检查元素时,我可以看到这些元标签,但当我通过WhatsApp分享链接(在其中我动态设置了元标签)时,描述、标题和图像不显示。有人能建议我哪里出错了吗?我正在使用Angular 2开发。
这是我在组件中设置元标签的方式。
var link = <HTMLMetaElement>document.createElement('meta');
link.setAttribute('property', 'og:url');
link.content = "https://www.test.com/";
document.getElementsByTagName('head')[0].appendChild(link);
var link = <HTMLMetaElement>document.createElement('meta');
link.setAttribute('property', 'og:description');
link.content = "测试描述";
document.getElementsByTagName('head')[0].appendChild(link);
var link = <HTMLMetaElement>document.createElement('meta');
link.setAttribute('property', 'og:image');
link.content = "https://imgurl";
document.getElementsByTagName('head')[0].appendChild(link);
英文:
I can set the meta tag dynamically and when I inspect the element then I can see those meta tags but when I share the link (where I set meta tag dynamically) via whats-app then description, title, and image are not showing. Can anyone suggest to me that where I am wrong? I am working on Angular 2.
this is how I set the meta tag in the component.
var link = <HTMLMetaElement>document.createElement('meta');
link.setAttribute('property', 'og:url');
link.content = "https://www.test.com/";
document.getElementsByTagName('head')[0].appendChild(link);
var link = <HTMLMetaElement>document.createElement('meta');
link.setAttribute('property', 'og:description');
link.content = "test description";
document.getElementsByTagName('head')[0].appendChild(link);
var link = <HTMLMetaElement>document.createElement('meta');
link.setAttribute('property', 'og:image');
link.content = "https://imgurl";
document.getElementsByTagName('head')[0].appendChild(link);
答案1
得分: 2
客户端动态添加/更改元标签将不会产生任何效果。这是因为Facebook、Twitter、WhatsApp等不会运行脚本以创建丰富的对象链接。
如果您的Web应用程序需要执行相同的操作,您应该在服务器端进行渲染。可以通过运行以下命令将Angular应用程序更改为服务器端渲染:ng add @angular/universal
然后在页面级组件上,使用名为Meta的服务。Angular Meta 服务文档
英文:
Dynamic adding/changing of meta tags on client side will have no effect. This is because Facebook, twitter, whatsapp, etc. do not run the scripts in order to create rich object link.
If your web app requires to do the same, you should do such rendering on the server side. An angular app can be changed to server side rendering by running the command ng add @angular/universal
Then on a page level component, use the service called Meta. Angular Meta service documentation
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论