简体   繁体   English

react-map-gl - 环境变量不传递令牌

[英]react-map-gl - Enviromental variable doesn't pass a token

DEVELOPER SERVER if I copy and paste my api token directly to .env开发服务器,如果我将我的 api 令牌直接复制并粘贴到 .env

NEXT_PUBLIC_MAPBOX_API="my-secret-mapbox-gl-api-key"

and use it like并像使用它一样

mapboxAccessToken={process.env.NEXT_PUBLIC_MAPBOX_API}

and it doesn't render the map and there is an error in dev tools并且它不渲染地图并且开发工具中存在错误

next-dev.js?3515:24 Error: An API access token is required to use Mapbox GL. See https://www.mapbox.com/api-documentation/#access-tokens-and-token-scopes
    at t.RequestManager._makeAPIURL (mapbox-gl.js?0c80:31:423135)
    at t.RequestManager.normalizeStyleURL (mapbox-gl.js?0c80:31:420958)
    at ei.loadURL (mapbox-gl.js?0c80:35:102600)
    at Map._updateStyle (mapbox-gl.js?0c80:35:458753)
    at Map.setStyle (mapbox-gl.js?0c80:35:458172)
    at new Map (mapbox-gl.js?0c80:35:448583)
    at Mapbox._initialize (mapbox.js?699b:222:1)
    at new Mapbox (mapbox.js?699b:128:1)
    at eval (map.js?48e9:61:1)

I tried to run a dev server again it hasn't helped我尝试再次运行开发服务器,但没有帮助

I use "next": "12.2.2".我使用“下一个”:“12.2.2”。

hmm?唔? I've also tried REACT_APP_MAPBOX_ACCESS_TOKEN instead of NEXT_PUBLIC then in Map component the prop: mapboxAccessToken={process.env.REACT_APP_MAPBOX_ACCESS_TOKEN} and it still hasn't helped!我也尝试过REACT_APP_MAPBOX_ACCESS_TOKEN而不是NEXT_PUBLIC然后在Map组件中使用 prop: mapboxAccessToken={process.env.REACT_APP_MAPBOX_ACCESS_TOKEN}但它仍然没有帮助! :/ :/

     <Map
              initialViewState={{
                longitude: 21.338631,
                latitude: 50.102242,
                zoom: 11,
              }}
              style={{
                overflow: "hidden",
              }}
              mapStyle={mapboxThemeStyle}
              mapboxAccessToken="my-secret-api-token-without-env-and-it-works" // 
              id="map"
              tabIndex="-1"
            >
              <Marker
                longitude={21.338631}
                latitude={50.102242}
                color="#206ea0"
              />
            </Map>

Tried add env to .env.local like NEXT_PUBLIC_MY_MAPBOX_API_TOKEN="pk.xxxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxx-xxxxxxxxxx" and it works!尝试将 env 添加到.env.local ,例如NEXT_PUBLIC_MY_MAPBOX_API_TOKEN="pk.xxxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxx-xxxxxxxxxx"并且有效! thank you @Heet Vakharia!谢谢@Heet Vakharia!

react-map-gl: https://github.com/visgl/react-map-gl react-map-gl: https ://github.com/visgl/react-map-gl

According to Next.JS docs for envs in Frontend, you need to add your envs in the .env.local file and All ENVS should start with NEXT_APP_ (which you have already done ) and then don't forget to restart the app in terminal根据 Next.JS docs for envs in Frontend,您需要在.env.local文件中添加您的 envs 并且所有 ENVS 都应该以NEXT_APP_ (您已经完成)开头,然后不要忘记在终端中重新启动应用程序

https://nextjs.org/docs/basic-features/environment-variableshttps://nextjs.org/docs/basic-features/environment-variables

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM