How to load Facebook Javascript sdk in Polymer Project?

点点圈 提交于 2019-12-24 07:00:40

问题


I know there is a web component out there called facebook-login and I don't want to install for the simple matter...

I tried adding Facebook Javascript SDK in web component. In the ready call I added basic javascript sdk.

ready() {

  window.fbAsyncInit = function() {
    FB.init({
      appId            : 'your-app-id',
      autoLogAppEvents : true,
      xfbml            : true,
      version          : 'v2.10'
    });
    FB.AppEvents.logPageView();
  };

  (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/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));
}

And then in my I tried adding this div but no luck.

<div class="fb-login-button" data-max-rows="1" data-size="large" data-button-type="continue_with" data-show-faces="false" data-auto-logout-link="false" data-use-continue-as="false"></div>'

Please point me to the right direction.

Updated: landing-app.html

<link rel="import" href="../../bower_components/polymer/polymer-element.html">
<link rel="import" type="" href="../../bower_components/app-layout/app-toolbar/app-toolbar.html">
<link rel="import" type="" href="../../bower_components/paper-icon-button/paper-icon-button.html">
<link rel="import" href="../../bower_components/iron-icons/iron-icons.html">
<link rel="import" href="../../bower_components/paper-button/paper-button.html">

<dom-module id="landing-app">
  <template>
    <style>
      :host {
        display: block;
      }
    </style>

    <!-- Navigation using App Toolbar -->
    <app-toolbar>
      <div class="toolbar-container">
        <div main-title>***</div>
      </div>
    </app-toolbar>
    <section>
      <p>Super Charged Bot</p>
      <h2>Helps pages to sell items through Messenger.</h2>
      <div class="fb-login-button" data-max-rows="1" data-size="large" data-button-type="continue_with" data-show-faces="false" data-auto-logout-link="false" data-use-continue-as="false"></div>
    </section>
  </template>

  <script>
    /**
    * @customElement
    * @polymer
    */
    class LandingApp extends Polymer.Element {
      static get is() { return 'landing-app'; }
      static get properties() {
        return {
          route: {
            prefix: '',
            path: ''
          }
        };
      }
      ready() {

        window.fbAsyncInit = function() {
          FB.init({
            appId            : 'your-app-id',
            autoLogAppEvents : true,
            xfbml            : true,
            version          : 'v2.10'
          });
          FB.AppEvents.logPageView();
        };

        (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/sdk.js";
            fjs.parentNode.insertBefore(js, fjs);
          }(document, 'script', 'facebook-jssdk'));
      }
    }

    window.customElements.define(LandingApp.is, LandingApp);
  </script>
</dom-module>

回答1:


note

( Just a good to know) The facebook SDK adder that you have copied over to the ready function, essentially creates a script tag with the src pointing to connect.fb.com as the very first script tag on the document. you are not scoping it to the component your are writing.

  • where did you add that <div>?

    Inside the shadow DOM? - If so, the javascript has NO access to your div

Try creating a <slot> inside your element's shadow DOM

Something like so:

<dom-module id="my-app">
    <template>
       <!-- your element's shadow DOM -->
      <slot name="fb-like-frame" ></slot>
    </template>
</dom-module>

then, on the page where you are connecting your element, just write

<my-app>
    <div slot="fb-like-frame" >
        <!--Insert your div here-->
    </div>
</my-app>

Plunker

I have created a plunk .I have used your own code. Except,

  • using slots.
  • A dummy app id

Look at both index.html and landing-app.html

from the nav bar on the left hand side.

  • Have you coded using <slots> ?

  • Are you calling super.ready() inside ready()?

View the component displaying the button



来源:https://stackoverflow.com/questions/45403009/how-to-load-facebook-javascript-sdk-in-polymer-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!