简体   繁体   English

如何将嵌套的 const 导出到另一个组件

[英]How export nested const to another component

I've got problem about how export nested const 'mapObj' in below example, because i need this in another component to get some information from this object.我在下面的示例中遇到了关于如何导出嵌套 const 'mapObj' 的问题,因为我需要在另一个组件中使用它来从这个对象中获取一些信息。 When I want to export nested const then I've got error that you can export only on the top.当我想导出嵌套的 const 时,我有一个错误,您只能在顶部导出。

First component:第一个组件:

const Mapper = ({
                    componentId,
                    width,
                    height,
                    xRenderCoord,
                    yRenderCoord,
                    zoom,
                    projection,
                    markerLayers = new TileLayer({
                        source: new OSM()
                    })
                }) => {
    const initMap = () => {
      const mapObj = new Map({
            controls: controls,
            target: componentId === '' ? 'map' : componentId,
            layers: [
                new TileLayer({
                    source: new OSM()
                }),
                markerLayers
            ],
            view: new View({
                projection: projection,
                center: fromLonLat([xRenderCoord, yRenderCoord]),
                zoom: zoom
            })
        })
    }

    useEffect(() => {
        initMap()
    }, [])

    return (
        <React.Fragment>
            <div
                id={componentId === '' ? 'map' : componentId}
                style={{
                    width: width,
                    height: height
                }}
            />
        </React.Fragment>
    )
}

export default Mapper

second component:第二个组成部分:

const MapService = (props) => {

async function vectorLoader(data) {
        var geoJsonFormat = new GeoJSON();
        var features = geoJsonFormat.readFeatures(data, {
            featureProjection: mapObj.getView().getProjection()
        })
    }

 return (
        <>
            <Mapper
                componentId='map'
                width='800px'
                height='800px'
                xRenderCoord={19}
                yRenderCoord={52}
                zoom={6}
                projection='EPSG:3857'
                
            />
        </>
    )
}

Do you have any idea to solve this problem?你有什么想法来解决这个问题吗?

You can't export something that isn't defined at the top level of a module.您不能导出未在模块顶层定义的内容。 In your case, mapObj is defined within the initMap function, which means that A) It may never exist at all (if the function is never called), which makes it hard to export.在您的情况下, mapObj是在initMap函数中定义的,这意味着 A)它可能根本不存在(如果从未调用过该函数),这使得导出变得困难。 :-) And B) Multiple mapObj s may exist (one for each time initMap is called.) :-) 和 B) 可能存在多个mapObj s(每次调用initMap一个。)

So if you want to export it, you'll have to do that by defining it at the top level of the module, if feasible.所以如果你想导出它,你必须在模块的顶层定义它,如果可行的话。 It doesn't immediately look like it's feasible, given that it appears to depend on the values of parameters to the Mapper function (and some controls variable that isn't declared in the code shown).考虑到它似乎取决于Mapper函数的参数值(以及一些未在所示代码中声明的controls变量),它看起来并不立即可行。

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

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