Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用通过<;安装的库;脚本>;反应中的标签_Javascript_Html_Reactjs_Typescript_Facebook Instant Games - Fatal编程技术网

Javascript 如何使用通过<;安装的库;脚本>;反应中的标签

Javascript 如何使用通过<;安装的库;脚本>;反应中的标签,javascript,html,reactjs,typescript,facebook-instant-games,Javascript,Html,Reactjs,Typescript,Facebook Instant Games,我正试图在React中创建一个Facebook即时HTML5应用程序 按照他们的要求,他们希望我使用脚本标记安装他们的SDK,如下所示: <script src="https://connect.facebook.net/en_US/fbinstant.6.3.js"></script> 它们还提供以下代码段: // Once all assets are loaded, tells the SDK // to end loading view and start t

我正试图在React中创建一个Facebook即时HTML5应用程序

按照他们的要求,他们希望我使用脚本标记安装他们的SDK,如下所示:

<script src="https://connect.facebook.net/en_US/fbinstant.6.3.js"></script>
它们还提供以下代码段:

// Once all assets are loaded, tells the SDK 
// to end loading view and start the game
FBInstant.startGameAsync()
  .then(function() {
  // Retrieving context and player information can only be done
  // once startGameAsync() resolves
  var contextId = FBInstant.context.getID();
  var contextType = FBInstant.context.getType();

  var playerName = FBInstant.player.getName();
  var playerPic = FBInstant.player.getPhoto();
  var playerId = FBInstant.player.getID();

  // Once startGameAsync() resolves it also means the loading view has 
  // been removed and the user can see the game viewport

  game.start();
});
我把它放在
src/index.tsx'

这就给了我错误,说:

  'FBInstant' is not defined  no-undef
这可能意味着库没有被正确安装/引入正确的名称空间以便我的React代码能够访问它

我怎样才能避开这件事?Facebook告诉我不要下载并包含它——他们会拒绝我的应用

重要注意事项:

不要下载SDK并将其添加到您的捆绑包中,因为它将在以后的步骤中被拒绝

这是一种在Facebook上构建游戏的新方法,不支持Graph API


因此,似乎我必须使用这些
标记。在
index.html

src/index.tsx
中,将其添加到顶部(在代码段之前):


正如您所解释的,之所以出现此错误,是因为typescript编译器无法识别
FBInstant
,因为它尚未安装

然而,在这种情况下,您所需要的只是让编译器忽略这些警告。我认为最合适的方法是将它导出到一个单独的
js
文件(而不是
ts
/
tsx
),因为它是一个javascript代码。然后将其作为任何节点模块导入

如果您不喜欢此选项,可采用以下替代方法:

  • 脚本
    标记之后的
    index.html
    中包含SDK代码
  • 使用
    @ts ignore
    抑制打字脚本错误
  • 在SDK脚本之前定义
    FBInstant
    (更多信息)

  • 也许试着把它添加到head Tag中我认为这篇文章会对你有很大帮助:
      'FBInstant' is not defined  no-undef
    
    const FBInstant = window.FBInstant;
    
    interface Window {
      [key:string]: any; // Add index signature
    }
    const FBInstant = (window as Window)['FBInstant'];