简体   繁体   English

Angular 2 - 在Facebook上共享页面URL,标题和描述

[英]Angular 2 - Share page URL, Title, and Description on Facebook

My end goal is simple: 我的最终目标很简单:

  1. The user clicks some button on the UI. 用户单击UI上的某个按钮。
  2. The Typescript function called by click opens a new share tab on facebook for the user. 通过所谓的打字稿功能click打开在Facebook上的用户一个新的共享选项卡。
  3. Both the 'Title' and the 'Description' for the shared page is provided by my site. 共享页面的“标题”和“描述”均由我的网站提供。

We have a post on including metatags on the page being linked, which fb the knows to include as the title/description ( How do I customize Facebook's sharer.php ). 我们在链接的页面上有一个包含元标记的帖子,fb知道包括作为标题/描述( 如何自定义Facebook的sharer.php )。 The problem is that I am using Angular 2, so I have to somehow to dynamically add metatags for the page before facebook sees it. 问题是我使用的是Angular 2,所以我必须以某种方式在facebook看到它之前为页面动态添加元标记。

I am having a hard time imagining how that works, since I am assuming the FB server will hit my NG2 app and search for the metatags (so editing metatags in the browser opening the share link is meaningless, since the FB API will get a different instance of the html). 我很难想象它是如何工作的,因为我假设FB服务器将点击我的NG2应用程序并搜索元标记(因此在浏览器中打开共享链接的元数据是没有意义的,因为FB API会有所不同html的实例)。

tl;dr: How do I open a fb url share dialog from an NG2 app and provide a title/description? tl; dr:如何从NG2应用程序打开fb url共享对话框并提供标题/说明?

Note: The 'Share on fb' page can simply be opened like this: window.open('http://www.facebook.com/sharer/sharer.php?u=www.google.com'); 注意:“在fb上分享”页面可以像这样打开: window.open('http://www.facebook.com/sharer/sharer.php?u=www.google.com'); This works, but without params. 这有效,但没有参数。


Optional addendum (sample code to add meta-tags dynamically, which works, but doesn't help): 可选附录(动态添加元标记的示例代码,有效,但无效):

var titleMeta = document.createElement('meta');
var descMeta = document.createElement('meta');

titleMeta.setAttribute('property', 'og:title');
titleMeta.setAttribute('content', 'The Rock');

descMeta.setAttribute('property', 'og:description');
descMeta.setAttribute('content', 'Foo Description');

document.getElementsByTagName('head')[0].appendChild(titleMeta);
document.getElementsByTagName('head')[0].appendChild(descMeta);

Addendum 2: The sharer used to allow you to put in the title and the description in the url, but that is no longer the case as per https://developers.facebook.com/x/bugs/357750474364812/ . 附录2:共享者曾经允许您在网址中输入标题和说明,但根据https://developers.facebook.com/x/bugs/357750474364812/不再是这种情况。 Looks like it HAS to be pulled from the meta tags. 看起来它必须从元标记中提取。

You Should look @ Share Buttons might help 你应该看看@ Share Buttons可能有所帮助

npm install --save ngx-sharebuttons

AppModule 的AppModule

import {ShareButtonsModule} from 'ngx-sharebuttons';
@NgModule({
  imports: [
   //...
   HttpModule, 
   ShareButtonsModule.forRoot(),
  // ...
  ]
})

Template 模板

<share-buttons></share-buttons>

The problem is that Facebook crawler will only see server side rendered HTML, Facebook will not execute client side javascript. 问题是Facebook爬虫只会看到服务器端渲染的HTML,Facebook不会执行客户端javascript。 That is the reason your code to update Meta tags won't help at all. 这就是您更新Meta标签的代码根本无济于事的原因。

Options- 选项 -

1) Look at options for server side rendering like phantom.js 1)查看服务器端渲染的选项,如phantom.js

2) If it's only one title and description through out your whole application then put meta tag directly to you root index.html(where we specify Base Href and loads app, vendor javascript bundles). 2)如果整个应用程序中只有一个标题和描述,那么将meta标签直接放到root index.html(我们指定Base Href并加载app,供应商javascript包)。 As pointed by @Stuart in comment 正如@Stuart所评论的那样

Try the following code - 请尝试以下代码 -

var windowObj = window.open();
windowObj.document.head.innerHTML='<meta property="og:title" content="The Rock"/><meta property="og:type" content="movie"/><meta property="og:url" content="http://www.imdb.com/title/tt0117500/"/><meta property="og:image" content="http://ia.media-imdb.com/rock.jpg"/><meta property="og:site_name" content="IMDb"/><meta property="fb:admins" content="USER_ID"/><meta property="og:description"      content="A group of U.S. Marines, under command of               a renegade general, take over Alcatraz and               threaten San Francisco Bay with biological               weapons."/>'

If you are using client side rendering, Facebook crawler can't execute js on page, so it gets HTML which is returned from server and searches for OG meta tags. 如果您使用客户端渲染,Facebook爬虫无法在页面上执行js,因此它获取从服务器返回的HTML并搜索OG元标记。

  • if it is dynamically rendered content, you need to on server side add those meta tags to index.html you are returning. 如果它是动态呈现的内容,则需要在服务器端将这些元标记添加到您要返回的index.html (i am not sure what you are using on backend to serve/generate index but you can use for example handlebars.js ) (我不确定你在后端使用什么来提供/生成索引,但你可以使用例如handlebars.js
  • otherwise just put those meta tags directly into your index.html 否则只需将这些元标记直接放入index.html

  • you can then test it here → https://developers.facebook.com/tools/debug/sharing 然后你可以在这里测试→ https://developers.facebook.com/tools/debug/sharing

if this can help you to modify the title description? 如果这可以帮助您修改标题说明? angular-2-seo 角-2- SEO

If you are using Angular 2, the dynamic meta tags before HTML rendering can not be possible for client side rendering with Angular 2 can not be possible. 如果您使用的是Angular 2,则无法使用Angular 2进行HTML渲染之前的动态元标记进行客户端渲染。 With Angular 2, it is possible only on server side rendering. 使用Angular 2,只能在服务器端渲染。

It is resolved in Angular 4. You can see on this link- 它在Angular 4中得到解决。您可以在此链接上看到 -

Click Here 点击这里

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

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