react-google-maps - 属性'方向'不存在

时间:2018-06-14 05:29:54

标签: reactjs google-maps react-google-maps

当我尝试运行react-google-maps的示例代码时遇到此错误:

Property 'directions' does not exist on type '{ children?: ReactNode; }'.

我正在尝试运行directions renderer。这是代码:

import * as React from 'react';
import './App.css';

import { compose, withProps, lifecycle } from "recompose"

import { withGoogleMap, withScriptjs, GoogleMap, DirectionsRenderer } from "react-google-maps"

const MapWithADirectionsRenderer = compose(
    withProps({
        googleMapURL: "https://maps.googleapis.com/maps/api/js?key=AIzaSyC4R6AN7SmujjPUIGKdyao2Kqitzr1kiRg&v=3.exp&libraries=geometry,drawing,places",
        loadingElement: <div style={{ height: `100%` }} />,
        containerElement: <div style={{ height: `400px` }} />,
        mapElement: <div style={{ height: `100%` }} />,
    }),
    withScriptjs,
    withGoogleMap,
    lifecycle({
        componentDidMount() {
            const DirectionsService = new google.maps.DirectionsService();

            DirectionsService.route({
                origin: new google.maps.LatLng(41.8507300, -87.6512600),
                destination: new google.maps.LatLng(41.8525800, -87.6514100),
                travelMode: google.maps.TravelMode.DRIVING,
            }, (result, status) => {
                if (status === google.maps.DirectionsStatus.OK) {
                    this.setState({
                        directions: result,
                    });
                } else {
                    console.error(`error fetching directions ${result}`);
                }
            });
        }
    })
)(props =>
    <GoogleMap
        defaultZoom={7}
        defaultCenter={new google.maps.LatLng(41.8507300, -87.6512600)}
    >
        {props.directions && <DirectionsRenderer directions={props.directions} />}
    </GoogleMap>
);



class App extends React.Component {

     public render() {
        return (
          <div className="App">
              <MapWithADirectionsRenderer />
          </div>
        );
  }
}

export default App;

恐怕我是新手做出反应的方式,我不确定是什么导致了这个错误。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

要消除此类错误,请尝试明确指定地图组件属性。

就此而言,可以引入以下类型:

type MapProps = {
    children?: React.ReactNode,
    directions: google.maps.DirectionsResult
};

然后是这样定义的地图组件:

const Map = (props: MapProps) => {
    return (
        <GoogleMap
            defaultZoom={7}
            defaultCenter={new google.maps.LatLng(41.8507300, -87.6512600)}
        >
            {props.directions && <DirectionsRenderer directions={props.directions} />}
        </GoogleMap>
    );
}

const MapWithADirectionsRenderer = compose(
    withProps({
        containerElement: <div style={{ height: `400px` }} />,
        googleMapURL: "https://maps.googleapis.com/maps/api/js?key=AIzaSyC4R6AN7SmujjPUIGKdyao2Kqitzr1kiRg&v=3.exp&libraries=geometry,drawing,places",
        loadingElement: <div style={{ height: `100%` }} />,
        mapElement: <div style={{ height: `100%` }} />,
    }),
    withScriptjs,
    withGoogleMap,
    lifecycle({
        componentDidMount() {
            const DirectionsService = new google.maps.DirectionsService();


            DirectionsService.route({
                destination: new google.maps.LatLng(41.8525800, -87.6514100),
                origin: new google.maps.LatLng(41.8507300, -87.6512600),
                travelMode: google.maps.TravelMode.DRIVING,
            }, (result, status) => {
                if (status === google.maps.DirectionsStatus.OK) {
                    this.setState({
                        directions: result,
                    });
                } else {
                    console.log(`error fetching directions ${result}`);
                }
            });
        }
    })
)(Map);

<强>更新

另一种选择是像props

一样投射(props as any).directions

实施例

<GoogleMap
    defaultZoom={7}
    defaultCenter={new google.maps.LatLng(41.8507300, -87.6512600)}
>
    {(props as any).directions && <DirectionsRenderer directions={(props as any).directions} />}
</GoogleMap>