【问题标题】:Google-Maps-React: Problems passing props to App.jsGoogle-Maps-React:将道具传递给 App.js 的问题
【发布时间】:2020-01-13 19:06:49
【问题描述】:

我正在开发一个网站,我想通过 Google-Maps-React 包使用 google maps API。 我之前测试过它,作为一个单一的应用程序,我成功地从 API 获取数据并将其呈现在屏幕上。现在我试图在我的实际项目中使用它,但它不起作用。我想问题可能是我没有正确地将道具从一个组件传递到另一个组件。

任何有关如何解决此问题的建议将不胜感激。非常感谢你!

这是我的两个组件和我在控制台上收到的消息(Webpack 编译成功):

index.js:139 未捕获的错误:您必须包含 google 属性 在新地图 (index.js:139)

Venue.js 持有实际地图

import { Map, GoogleApiWrapper, Marker } from "google-maps-react";

const mapStyles = {
    width: '100%',
    heigth: '100%',
};

export class MapContainer extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            venues: [
                { lat: 52.471015, lng: 12.719707 }
            ]
        }
    }

    displayMarkers = () => {
        return this.state.venues.map((venue, index) => {
          return <Marker key={index} id={index} position={{
            lat: venue.latitude,
            lng: venue.longitude
          }}
          onClick={() => console.log(this.state.venues)} />
        })
      }

      render() {
        return (
          <Map
           google={this.props.google}
           zoom={8}
           style={mapStyles}
           initialCenter={{ lat: 52.471015, lng: 12.719707 }}
          >
            {this.displayMarkers()}

          </Map>
        );
      }
    }


export default GoogleApiWrapper({
    apiKey:''
  })(MapContainer);

App.js

import React from "react";
import { BrowserRouter, Route, Link } from "react-router-dom";
import Main from "./main";
import MapContainer from "google-maps-react";


export class App extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            error: false,
        };        
    }

    componentDidMount() {
        console.log("App mounted");

    }

    render() {
        return (
            <BrowserRouter>
                <div>
                    <Route exact path="/" component={Main} />
                    <Route exact path="/venue" component={MapContainer} />
                </div>
            </BrowserRouter>
        );
    }
}

【问题讨论】:

  • 你设置了密钥吗?
  • 尝试将 window.google 而不是 this.props.google 传递给您的 Map 组件。
  • @FábioBCSouza 是的,我做到了。我刚刚在此处发布代码之前将其删除了 :)
  • @SultanH。只是做了,但并没有真正解决问题。不过非常感谢!
  • 我认为问题可能出在 上。但老实说,我盯着这段代码看了很久,已经看不出它有什么问题了。

标签: javascript reactjs api google-maps react-props


【解决方案1】:

因为你的导入 MapContainer 路径不正确!

在您的App.js 文件更改中:

import MapContainer from "google-maps-react";

到您的 MapContainer 组件,例如:

import MapContainer from "./components/Venue.js";

简单演示:HERE

【讨论】:

  • 您好!我刚刚更改了它,但现在我遇到了 webpack 无法编译的问题:在 Object.parseStatementContent (/mnt/c/Users/pc/desktop/weddingproject/my-app/node_modules/@babel/parser/lib/index .js:10794:27) @ ./src/app.js 22:0-38 55:19-31 @ ./src/start.js @multi @babel/polyfill ./src/start.js webpack: 失败编译。
  • 我在代码沙箱中添加了您的代码,请参阅答案简单演示,如果您的源代码没有任何问题,您必须检查您的源代码可能需要停止并启动您的 npm,(答案已更新)只需要apiKey
  • 我可能必须在我的源代码上导入和实现 API 代码。我现在要试着去做。非常感谢!
猜你喜欢
  • 2018-05-15
  • 1970-01-01
  • 2014-03-12
  • 1970-01-01
  • 2019-10-07
  • 2021-06-28
  • 1970-01-01
  • 2018-08-01
  • 2016-03-19
相关资源
最近更新 更多