gpt4 book ai didi

javascript - 将第三方脚本导入 React

转载 作者:行者123 更新时间:2023-11-30 15:26:40 25 4
gpt4 key购买 nike

我正在尝试在 React 生态系统中使用第三方脚本 (mapbox)。在传统的 javascript 中,我导入脚本然后像这样初始化它:

<script src='https://api.mapbox.com/mapbox.js/v3.0.1/mapbox.js'></script>

L.mapbox.accessToken = 'pk.xxxtoken';
var map = L.mapbox.map('map', 'mapbox.streets').setView([40, -74.50], 9);

我尝试离开我的 React 应用程序的头部,然后在 componentDidMount() 中尝试初始化它,但它不知道“L”是什么:

'L' is not defined    

所以我需要以某种方式将脚本导入到组件中,所以我查看了“react-async-script”,但我不确定...

最佳答案

看起来 mapbox 有一个 npm 包,所以你可以用 npm install --save mapbox 安装它,然后通过导入它在你的 React 组件中使用它

import * as L from 'mapbox';

// do stuff with L here

关于javascript - 将第三方脚本导入 React,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42856966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com