【问题标题】:react-google-maps - Property 'directions' does not existreact-google-maps - 属性“方向”不存在
【发布时间】:2018-06-19 15:27:27
【问题描述】:

当我尝试运行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;

恐怕我是 react 的新手,我不确定是什么导致了这个错误。有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

    要消除这种错误,请尝试显式指定地图组件属性。

    为此,可以引入以下类型:

    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> 
    

    【讨论】:

    • 我像你一样添加了键入的参数,但我现在收到此错误:Parameter 'opts' implicitly has an 'any' type. 所以我将参数添加到 MapProps 但我仍然遇到错误。
    • @user664509,答案已更新为替代选项(更新部分)
    猜你喜欢
    • 2019-03-06
    • 1970-01-01
    • 2018-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 2018-02-19
    相关资源
    最近更新 更多