找不到名字' FB' [TS]

时间:2018-01-29 21:34:29

标签: facebook-graph-api react-tsx

我正在尝试初始化JavaScript SDK以最终使用Facebook Graph API。但它一直在说Cannot find name FB,不知道为什么。这是我做的,

componentDidMount() {
    (window as any).fbAsyncInit = function() {
        FB.init({ // this FB errors out
            appId            : "my-app-id",
            autoLogAppEvents : true,
            xfbml            : true,
            version          : "v2.11"
        });
    };

    (function(d: any, s: any, id: any) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) {return; }
    js = d.createElement(s); js.id = id;
    js.src = "https://connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
    }(document, "script", "facebook-jssdk"));
}

P.S。我使用的是TSLint,这就是我拥有所有typedef any

的原因

1 个答案:

答案 0 :(得分:0)

安装https://www.npmjs.com/package/@types/facebook-js-sdk并将facebook-js-sdk添加到typestsconfig.ts中的tsconfig.app.json中,如果使用Angular CLI时有嵌套应用程序。

示例tsconfig.app.json

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "outDir": "../../out-tsc/app",
    "types": [
      "node",
      "gapi", // google
      "gapi.auth2", // google
      "facebook-js-sdk" // facebook
    ]
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ]
}