Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用React查询小天气应用程序的字符串_Javascript_Reactjs_Jquery Selectors_Web Development Server - Fatal编程技术网

Javascript 使用React查询小天气应用程序的字符串

Javascript 使用React查询小天气应用程序的字符串,javascript,reactjs,jquery-selectors,web-development-server,Javascript,Reactjs,Jquery Selectors,Web Development Server,我希望能够使用诸如?latt_long=34.052235,-118.243683&&woeid=2442047之类的查询字符串为我的天气应用程序设置一个城市。这里有一个链接。现在我的项目中有一个cities.json文件,App.js从那里获取关于城市的数据。我似乎不知道如何使用查询字符串。它告诉我使用const queryString=require('query-string');为了使用查询字符串,但我不能在App.js中声明常量 我的App.js: import React, { C

我希望能够使用诸如?latt_long=34.052235,-118.243683&&woeid=2442047之类的查询字符串为我的天气应用程序设置一个城市。这里有一个链接。现在我的项目中有一个cities.json文件,App.js从那里获取关于城市的数据。我似乎不知道如何使用查询字符串。它告诉我使用const queryString=require('query-string');为了使用查询字符串,但我不能在App.js中声明常量

我的App.js:


import React, { Component } from "react";
import FrontSide from "./FrontSide";
import BackSide from "./BackSide";
import "./panel.css";
import cities from "./cities.json"
import queryString from 'query-string';


class App extends Component {

const queryString = require('query-string'); //I get unexpected token error (11:6) on this line right before queryString
console.log(location.search);

state = {flipped: false, currentCity: cities[0]};


 onFlip =() => {
    this.setState({flipped: !this.state.flipped});
 };

 onSelectCity = (city) => {
   this.setState({currentCity: city})
 }


  render() {
    return (
        <div className={`panel ${this.state.flipped ? 'flip' : ""}`}>
            <div className="panel-front">
            <FrontSide onClick={this.onFlip} currentCity={this.state.currentCity}/>
          </div>
            <div className="panel-back">
            <BackSide
              cities={cities}
              onClick={this.onFlip}
              currentCity={this.state.currentCity}
              onSelect={this.onSelectCity}
              />
          </div>
        </div>
      );
  }
}

export default App;
我试着宣布 const queryString=require('query-string'); 但react在“queryString”处显示意外标记

请参考我的github链接,在那里你可以找到App.js和cities.json文件

我希望从URL查询字符串中获得有关城市的信息以显示在我的前端

这就是我得到的错误:

Failed to compile.

./src/App.js
Syntax error: Unexpected token (11:6)

   9 | class App extends Component {
  10 |
> 11 | const queryString = require('query-string');
     |       ^
  12 | console.log(location.search);
  13 |
  14 | state = {flipped: false, currentCity: cities[0]};







只需远程调用
constquerystring=require('query-string')从类声明中划出一行,并将其置于顶部。就在导入语句的正下方,一切正常。React不喜欢类声明中的require语句

请将代码发布在此处,而不是外部链接后面。@ColinRicardo sure:)是否尝试从“查询字符串”导入查询字符串
。正如我所知,这个模块是一个
NodeJS
模块,不用于React。是的,尝试使用
qs
模块可能是个好主意。@dqlgnoleht我正在尝试使用queryString,但我不确定为什么我不能在我的App.js中声明const queryString
Failed to compile.

./src/App.js
Syntax error: Unexpected token (11:6)

   9 | class App extends Component {
  10 |
> 11 | const queryString = require('query-string');
     |       ^
  12 | console.log(location.search);
  13 |
  14 | state = {flipped: false, currentCity: cities[0]};