【问题标题】:Using Facebook SDK in Angular 2在 Angular 2 中使用 Facebook SDK
【发布时间】:2016-07-28 19:49:40
【问题描述】:

我一直在读这个:https://developers.facebook.com/docs/sharing/reference/share-dialog

我需要导入 facebook SDK 才能使用以下代码:

FB.ui({
  method: 'share',
  href: 'https://developers.facebook.com/docs/',
}, function(response){});

如何使用 Angular 2 应用导入 facebook SDK?

【问题讨论】:

    标签: angular facebook-javascript-sdk


    【解决方案1】:

    无需使用 facebook sdk,我们可以在同一个 ts 文件中使用窗口位置,如下所示。我们也可以对任何社交媒体(如 twitterlinkedin)做同样的事情。

    脸书:

    popupfacebook() {
        let url = 'http://www.facebook.com/sharer.php?u='+ this.shareLink;
           let newwindow=window.open(url,'name','height=500,width=520,top=200,left=300,resizable');
      if (window.focus) {
          newwindow.focus()
      } 
    }
    

    推特:

    popuptweet(){
           let url = 'https://twitter.com/intent/tweet?text='+ this.shareLink;
           let newwindow=window.open(url,'name','height=500,width=520,top=200,left=300,resizable');
      if (window.focus) {
          newwindow.focus()
    }}
    

    领英:

    popuplinkedin(){
          let url ='https://www.linkedin.com/shareArticle?mini=true&url='+this.shareLink;
           let newwindow=window.open(url,'name','height=500,width=520,top=200,left=300,resizable');
      if (window.focus) {
          newwindow.focus()
      }
    }
    

    【讨论】:

    • 比使用任何包更好的解决方案。
    • 有没有类似的方式分享到Instagram?谢谢
    • 人们来到这里寻找解决将 Facebook SDK 与 Angular 集成的孤立问题的解决方案。您完全无法为此提供答案。
    【解决方案2】:

    经过三天的研究,找到了 facebook 分享动态内容的解决方案

    在 index.html 中:

    <meta property="og:type" content="website" />
    <meta name="fbDescription" property="og:description" content="desc">
    <meta name="fbJobTitle" property="og:title" content="title">
    <meta property="fb:app_id" content="ur app id" />
    <meta name="fbImage" property="og:image" content="url">
    <meta property="og:site_name" content="site name" />
    

    安装 npm 包

    npm i --save ngx-facebook
    

    在你的 app.module 中

    import { FacebookModule } from 'ngx-facebook';
    imports:[FacebookModule.forRoot()]   // donot forget to add
    

    在您的服务或组件中添加

    import { Meta } from '@angular/platform-browser';
    providers: [ApiService, CommonDataService, FacebookService]
    
    constructor(private fbk: FacebookService){
    fbk.init({
        appId: 'yourappid', cookie: true, status: true, xfbml: true, version: 'v2.8'
      });
     }
      (function (d, s, id) {
      var js, fjs = d.getElementsByTagName(s)[0];
      if (d.getElementById(id))
        return;
      js = d.createElement(s);
      js.id = id;
      js.src = "//connect.facebook.net/en_US/all.js";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
    

    在你的按钮点击函数中添加

    this.meta.updateTag({ property: 'og:type', content: 'website' });
    this.meta.updateTag({ property: 'og:site_name', content: 'websitename' });
              this.meta.updateTag({ property: 'og:title', content: 'title'});
              this.meta.updateTag({ property: 'og:description', content: 'Description'});
              this.meta.updateTag({ property: 'og:image', content: 'url' });
    
      this.fbk.ui({
        method: 'share_open_graph',
        action_type: 'og.shares',
        action_properties: JSON.stringify({
          object: {
            'og:title': 'title',
            'og:description': 'description',
            'og:image': 'imagelink',
            'og:url': referlink,
          }
        })
      });
    

    参考网址:http://drib.tech/programming/dynamically-change-facebook-open-graph-meta-data-javascript

    希望这会有所帮助!

    【讨论】:

    • 这对我有用。您是否尝试在您管理的业务页面上共享?它仅适用于个人资料页面。
    • 它适用于共享动态内容,但我没有尝试使用商业页面。
    • 是的。它不适用于业务页面。一旦你有任何信息,请分享。
    • 你好@Priya,你找到解决fb分享问题的方法了吗?
    【解决方案3】:

    如果您想访问您的 any.component.ts 文件,您需要获得 window 参考(我使用以下代码):

    function _window():any {
      return window;
    }
    
    export class WindowRef {
        public static get():any {
            return _window();
        }
    }
    

    现在你可以:

    import { WindowRef } from './windowRef';
    
    export class AnyComponent {
       WindowRef.get().FB.ui // <-- this is how you'll reference it now
    }
    

    您需要确保FB 对象在window 上可用。之后您的代码将转译。

    【讨论】:

    • 您能更深入地解释一下吗?把第一块代码放在哪里?为什么这应该有效?
    • 第一个块将是一个自己的文件(我称之为 windowRef.js)。它包含一个可导入的类(参见第二个块)。但是你可以用更简单的方式做到这一点。把“声明变量窗口;”在您需要访问 FB.ui 函数的文件中导入之后。然后您应该能够调用 window.FB.ui 因为 FB 是窗口的对象。另一种方法可能是使用“declare var FB;”并直接访问相应的功能,但我不确定。为了解释,窗口对象代表全局范围,您可以从那里访问所有全局变量。
    • 声明 FB 变量对我有用,即 import { Component } from '@angular/core';声明 let FB: any;
    猜你喜欢
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    相关资源
    最近更新 更多