Javascript 如何使用通过<;安装的库;脚本>;反应中的标签
我正试图在React中创建一个Facebook即时HTML5应用程序 按照他们的要求,他们希望我使用脚本标记安装他们的SDK,如下所示: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
<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
抑制打字脚本错误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'];